home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 101 / CD-ROM 101.iso / compl / maya5ple / Install_MayaPLE5_English.exe / Maya / Data1.cab / fireworks.mel < prev    next >
Encoding:
Text File  |  2003-07-17  |  89.4 KB  |  2,742 lines

  1. // Copyright (C) 1997-2002 Alias|Wavefront,
  2. // a division of Silicon Graphics Limited.
  3. //
  4. // The information in this file is provided for the exclusive use of the
  5. // licensees of Alias|Wavefront.  Such users have the right to use, modify,
  6. // and incorporate this code into other products for purposes authorized
  7. // by the Alias|Wavefront license agreement, without fee.
  8. //
  9. // ALIAS|WAVEFRONT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  10. // INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  11. // EVENT SHALL ALIAS|WAVEFRONT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  12. // CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  13. // DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  14. // TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. // PERFORMANCE OF THIS SOFTWARE.
  16. //
  17. //
  18. //  Alias|Wavefront Script File
  19. //  MODIFY THIS AT YOUR OWN RISK
  20. //
  21. //    Creation Date:    April 1999
  22. //    Authors:        Carol Levy, Jon Southard, Rob Tesdahl
  23. //
  24. //
  25. //  ==================== fireworks.mel ==========
  26. //
  27. //  SYNOPSIS
  28. //      Create a fireworks clip effect.
  29. //
  30. //  CONTENTS
  31. //        
  32. //  UTILITY PROCEDURES
  33. //
  34. //        getUniqueName
  35. //        createColoredStreaks
  36. //
  37. //    PROCEDURES RELATED TO THE FIREWORKS GROUP
  38. //
  39. //        createFireworksAttributes
  40. //        createShaderIncandescenceExpressions
  41. //        connectFireworksAttributes
  42. //        
  43. //    PROCEDURES RELATED TO THE FIREWORKS ROCKETS
  44. //
  45. //        createRocketsExpression
  46. //        addAndConnectRocketAttributes 
  47. //        createRocketParticles
  48. //        hideAndLockRocketAttributes
  49. //        createRocketPositionParticles
  50. //        createRocketTrailsEmitter
  51. //        setBurstColorIndices 
  52. //        makeGravityLocatorsForRockets
  53. //      createRockets
  54. //
  55. //    PROCEDURES TO CREATE THE TRAIL SPARKS OF THE ROCKETS
  56. //
  57. //        createRocketTrailsExpression
  58. //        createRocketTrailsConeInstances
  59. //        createRocketTrails
  60. //
  61. //    PROCEDURES TO CREATE THE FIREWORK BURSTS
  62. //
  63. //        createBurstSparksExpression
  64. //        defaultFireworksColors  (In a separate mel script)
  65. //        createBurstSparksConeInstances
  66. //        createBurstSparks
  67. //        createRocketPositionLocators
  68. //
  69. //    PROCEDURES TO SET FIREWORKS ATTRIBUTES 
  70. //        (Called from performDynamicsClipEffects)
  71. //
  72. //        fwSetRocketAttributes
  73. //        fwSetTrailAttributes
  74. //        fwSetSparksAttributes
  75. //
  76. //  MAIN PROCEDURES:
  77. //
  78. //        createFireworks()
  79. //        fireworks()
  80. //
  81. //    PROCEDURES TO CREATE THE LIGHTS FROM THE BURSTS
  82. //
  83. //        createRocketTrailsGlow (Not yet implemented)
  84. //        createBurstSparksGlow (Not yet implemented)
  85. //
  86. //
  87.  
  88. // ************************************************************************ //
  89. //                                                                          //
  90. //                           UTILITY PROCEDURES                             //
  91. //                                                                          //
  92. // ************************************************************************ //
  93.  
  94. // Save the names of the shaders, so their glow intensity can be connected to
  95. // the glow intensity of the fireworks Group.
  96. //
  97. string $gFwTrailShaders[];
  98. string $gFwSparksShaders[];
  99.  
  100. //
  101. //  ================ getUniqueName ================
  102. //
  103. //  SYNOPSIS
  104. //      Return a name unique to the scene based on the argument by finding
  105. //        the smallest index N such that <name>N is a unqie name in the scene.
  106. //
  107. //    ARGUMENTS
  108. //        string $name -- the base name from which to determine a unique alternative
  109. //
  110. //    RETURN
  111. //        The unique name.
  112. //
  113. proc string getUniqueName(string $name)
  114. {
  115.     int $i = 1;
  116.  
  117.     int $nameExists = 1;
  118.     string $names[];
  119.     string $trialName;
  120.     while ($nameExists)
  121.     {
  122.         $trialName = ($name + $i);
  123.         $names = `ls $trialName`;
  124.         if (size($names) == 0)
  125.             $nameExists = 0;
  126.         else
  127.             $i ++;
  128.     }
  129.     return $trialName;
  130. }
  131.  
  132.  
  133. //
  134. //  ================ createColoredStreaks ================
  135. //
  136. //  SYNOPSIS
  137. //      Create a cone and its shader for one of the colored streaks of the
  138. //        firework sparks or trails.  Set the color and the glow intensity of
  139. //        the shader.  Connect the cone to the shader, lock the shader's
  140. //        color attribute and connect its ambient color from its color to make
  141. //        them the same.
  142. //
  143. //    ARGUMENTS
  144. //        string $sparksOrTrail    -- which object the streaks are created for
  145. //        string $coneName        -- the name of the cone
  146. //        string $shaderName        -- the name of the shader
  147. //        vector $coneScale        -- the scale of the cone
  148. //        vector $coneAxis        -- the axis along which to create the cone
  149. //        vector $rgb                -- the color (rgb) of the shader
  150. //
  151. //    RETURN
  152. //        The name of the cone and shader created.
  153. //
  154. proc string[]  createColoredStreaks(string $sparksOrTrail,
  155.                                     string $coneName, 
  156.                                     string $shaderName, 
  157.                                     vector $coneScale,
  158.                                     vector $coneAxis,
  159.                                     vector $rgb)
  160. {
  161.     global string $gFwTrailShaders[];
  162.     global string $gFwSparksShaders[];
  163.  
  164.     // Create the cone that will be instanced to the burst sparks particles to
  165.     // create one of the colored streaks of the fireworks.
  166.     //
  167.     string $result[];
  168.     $result = `cone -ax ($coneAxis.x) ($coneAxis.y) ($coneAxis.z) -n $coneName`;
  169.     string $cone = $result[0];
  170.     scale ($coneScale.x) ($coneScale.y) ($coneScale.z);
  171.     
  172.     // Create the shader for the cone we just made.
  173.     //
  174.     string $shader = `shadingNode -asShader lambert -n $shaderName`;
  175.     string $shaderSG = $shader + "SG";
  176.  
  177.     sets -renderable true -noSurfaceShader true -empty -name $shaderSG;
  178.     connectAttr -f ($shader + ".outColor")  
  179.                     ($shaderSG + ".surfaceShader");
  180.  
  181.     // Set the color of the cone to be the color sent in.
  182.     //
  183.     setAttr ($shader + ".color") 
  184.              -type double3   ($rgb.x) ($rgb.y) ($rgb.z);
  185.  
  186.     // Connect the ambient color to the shader color, so shaders will not
  187.     // be cast on the sparks.  We want the ambient color to be the same as
  188.     // the main color, so just connect it.
  189.     //
  190.     connectAttr ($shader + ".color") ($shader + ".ambientColor");
  191.  
  192.     // Lock the shader color so it can only be edited from the Attribute
  193.     // Editor's rocket shape.  We have to lock it rather than making a
  194.     // connection because there's a rendering bug: a direct connection
  195.     // to the shader's color is not reflected in hardware shading.
  196.     //
  197.     setAttr -lock true ($shader + ".color");
  198.  
  199.     // Set a glow intensity.
  200.     //
  201.     setAttr ($shader + ".glowIntensity")  0.3;
  202.  
  203.     // Add the shader name to the shader array.
  204.     //
  205.     if ($sparksOrTrail == "trail")
  206.     {
  207.         $gFwTrailShaders[size($gFwTrailShaders)] = $shader;
  208.     }
  209.     else
  210.     {
  211.         $gFwSparksShaders[size($gFwSparksShaders)] = $shader;
  212.     }
  213.  
  214.     // Connect the shader to the cone.
  215.     //
  216.     sets -e -forceElement $shaderSG $cone;
  217.  
  218.     // Return the name of the cone.
  219.     //
  220.     string $returns[2];
  221.     $returns[0] = $cone;
  222.     $returns[1] = $shader;
  223.     return $returns;
  224. }
  225.  
  226. // ************************************************************************ //
  227. //                                                                          //
  228. //                           FIREWORKS PROCEDURES                           //
  229. //                                                                          //
  230. // ************************************************************************ //
  231.  
  232. //  ================ createFireworksAttributes ================
  233. //
  234. //  SYNOPSIS
  235. //      Create the attributes on the fireworks group that the user will
  236. //        most likely want to modify to tune the fireworks effect.
  237. //        These are attributes on the various objects that are collected together
  238. //        under the fireworks group for ease of editing.  
  239. //
  240. //    ARGUMENTS
  241. //        string $fireworksGroup -- the top level group of the fireworks
  242. //
  243. //    RETURN
  244. //        None
  245. //
  246. proc createFireworksAttributes(string $fireworksGroup)
  247. {
  248.     if( `licenseCheck -type complete` == 0 )
  249.     {
  250.         warning("You are not licensed to use the Fireworks Effect.");
  251.         return;
  252.     }
  253.  
  254.     // ROCKETS ATTRIBUTES
  255.     //
  256.     addAttr -is true -ln "maxBurstSpeed" 
  257.             -at "float" -min 0 -dv 20 
  258.             $fireworksGroup;
  259.     addAttr -is true -ln "minSparksCount" 
  260.             -at "float" -min 0 -dv 100     
  261.             $fireworksGroup;
  262.     addAttr -is true -ln "maxSparksCount" 
  263.             -at "float" -min 0 -dv 200 
  264.             $fireworksGroup;
  265.     addAttr -is true -ln "sparksColorSpread" 
  266.             -at "float" -min 0 -dv 3 
  267.             $fireworksGroup;
  268.     addAttr -is true -ln "rocketGravity" 
  269.             -at "float" -min 0 -dv 9.8 
  270.             $fireworksGroup;
  271.     addAttr -ln showAllBurstPositions 
  272.             -at bool  
  273.             $fireworksGroup;
  274.     addAttr -ln showAllLaunchPositions 
  275.             -at bool  
  276.             $fireworksGroup;
  277.  
  278.     setAttr -keyable on ($fireworksGroup + ".maxBurstSpeed");
  279.     setAttr -keyable on ($fireworksGroup + ".minSparksCount");
  280.     setAttr -keyable on ($fireworksGroup + ".maxSparksCount");
  281.     setAttr -keyable on ($fireworksGroup + ".sparksColorSpread");
  282.     setAttr -keyable on ($fireworksGroup + ".rocketGravity");
  283.     setAttr -keyable on ($fireworksGroup + ".showAllBurstPositions");
  284.     setAttr -keyable on ($fireworksGroup + ".showAllLaunchPositions");
  285.  
  286.     // ROCKET TRAIL EMITTER ATTRIBUTES
  287.     //
  288.     addAttr -is true -ln "trailEmitRate" 
  289.             -at "float" -min 0  -dv 150 
  290.             $fireworksGroup;
  291.     addAttr -is true -ln "trailEmitSpeed" 
  292.             -at "float" -min 0  -dv .5 
  293.             $fireworksGroup;
  294.     addAttr -is true -ln "trailEmitSpread" 
  295.             -at "float" -min 0  -dv .5 
  296.             $fireworksGroup;
  297.  
  298.     setAttr -keyable on ($fireworksGroup + ".trailEmitRate");
  299.     setAttr -keyable on ($fireworksGroup + ".trailEmitSpeed");
  300.     setAttr -keyable on ($fireworksGroup + ".trailEmitSpread");
  301.  
  302.     // ROCKET TRAIL ATTRIBUTES
  303.     //
  304.     addAttr -is true -ln "trailMinTailSize" 
  305.             -at "float" -min 0  -dv .5 
  306.             $fireworksGroup;
  307.     addAttr -is true -ln "trailMaxTailSize" 
  308.             -at "float" -min 0  -dv 2 
  309.             $fireworksGroup;
  310.     addAttr -is true -ln "trailGlow" 
  311.             -at "float" -min 0  -dv .3 
  312.             $fireworksGroup;
  313.     addAttr -is true -ln "trailIncandescence" 
  314.             -at "float" -min 0  -dv .25 
  315.             $fireworksGroup;
  316.  
  317.     setAttr -keyable on ($fireworksGroup + ".trailMinTailSize");
  318.     setAttr -keyable on ($fireworksGroup + ".trailMaxTailSize");
  319.     setAttr -keyable on ($fireworksGroup + ".trailGlow");
  320.     setAttr -keyable on ($fireworksGroup + ".trailIncandescence");
  321.  
  322.     // BURST SPARKS ATTRIBUTES
  323.     //
  324.     addAttr -is true -ln "sparksMinTailSize" 
  325.             -at "float" -min 0 -dv .5 
  326.             $fireworksGroup;
  327.     addAttr -is true -ln "sparksMaxTailSize" 
  328.             -at "float" -min 0 -dv 2 
  329.             $fireworksGroup;
  330.     addAttr -is true -ln "sparksGlow" 
  331.             -at "float" -min 0  -dv .3 
  332.             $fireworksGroup;
  333.     addAttr -is true -ln "sparksIncandescence" 
  334.             -at "float" -min 0  -dv .25 
  335.             $fireworksGroup;
  336.  
  337.     // Add an attribute to control display of sparks as particles or
  338.     // cones.
  339.     //
  340.     addAttr -is true -ln "displayGeometry" -at bool -dv true $fireworksGroup;
  341.  
  342.     setAttr -keyable on ($fireworksGroup + ".sparksMinTailSize");
  343.     setAttr -keyable on ($fireworksGroup + ".sparksMaxTailSize");
  344.     setAttr -keyable on ($fireworksGroup + ".sparksGlow");
  345.     setAttr -keyable on ($fireworksGroup + ".sparksIncandescence");
  346.     setAttr -keyable on ($fireworksGroup + ".displayGeometry");
  347.  
  348. }
  349.  
  350. //  ================ createShaderIncandescenceExpressions ================
  351. //
  352. //  SYNOPSIS
  353. //      Create the expressions to set the incandescence of the trail and
  354. //        burst sparks shaders.  One expression for all the trail shaders and
  355. //        one expression for all the burst sparks shaders.
  356. //
  357. //    ARGUMENTS
  358. //        string $fireworksName    -- base fireworks name
  359. //        string $fireworksGroup   -- the top level fireworks group, which has the
  360. //                                    incandescence attribute the shaders'incancescence
  361. //                                    is being connect to
  362. //
  363. //    RETURN
  364. //        None
  365. //
  366. proc createShaderIncandescenceExpressions( string $fireworksName,
  367.                                             string $fireworksGroup)
  368. {
  369.     if( `licenseCheck -type complete` == 0 )
  370.     {
  371.         warning("You are not licensed to use the Fireworks Effect.");
  372.         return;
  373.     }
  374.  
  375.     // One for trails, one for sparks
  376.  
  377.     global string $gFwTrailShaders[];
  378.     global string $gFwSparksShaders[];
  379.  
  380.     // Set the name of the appropriate incandescence attribute depending on
  381.     // whether we are creating shaders for trails or burst sparks.
  382.     //
  383.     string $incan;
  384.     string $theExpression = "";
  385.  
  386.     // First do it for trails
  387.     //
  388.     // Make an expression for the incandescence of the trail shaders, if
  389.     // there are any.
  390.     //
  391.     if (size($gFwTrailShaders) > 0)
  392.     {
  393.         $incan = $fireworksGroup + ".trailIncandescence";
  394.  
  395.         $theExpression += "// Set the incandescence of the trail shaders.\n";
  396.         $theExpression += "// It is based on the shader color, with an\n";
  397.         $theExpression += "// incandescence intensity value added in.\n";
  398.         $theExpression += "//\n";
  399.  
  400.         for ($i = 0; $i < size($gFwTrailShaders); $i++)
  401.         {
  402.             string $shader = $gFwTrailShaders[$i];
  403.  
  404.             $theExpression += "float $r = " + $shader + ".colorR;\n";
  405.             $theExpression += "float $g = " + $shader + ".colorG;\n";
  406.             $theExpression += "float $b = " + $shader + ".colorB;\n";
  407.     
  408.             $theExpression += "vector $rgb = <<$r, $g, $b>>;\n";
  409.             $theExpression += "vector $hsv = rgb_to_hsv($rgb);\n";
  410.             $theExpression += "$hsv = <<$hsv.x, $hsv.y, " + $incan + ">>;\n";
  411.             $theExpression += "$rgb = hsv_to_rgb($hsv);\n";
  412.     
  413.             $theExpression += $shader + ".incandescenceR = $rgb.x;\n";
  414.             $theExpression += $shader + ".incandescenceG = $rgb.y;\n";
  415.             $theExpression += $shader + ".incandescenceB = $rgb.z;\n\n";
  416.         }
  417.  
  418.         expression -n ($fireworksName + "TrailShadersExpr") -s $theExpression;
  419.     }
  420.  
  421.     // Then do it for sparks.
  422.     //
  423.     //
  424.     if (size($gFwSparksShaders) > 0)
  425.     {
  426.         $incan = $fireworksGroup + ".sparksIncandescence";
  427.         $theExpression = "";
  428.  
  429.         $theExpression += "// Set the incandescence of the sparks shaders.\n";
  430.         $theExpression += "// It is based on the shader color, with an\n";
  431.         $theExpression += "// incandescence intensity value added in.\n";
  432.         $theExpression += "//\n";
  433.  
  434.         for ($i = 0; $i < size($gFwSparksShaders); $i++)
  435.         {
  436.             string $shader = $gFwSparksShaders[$i];
  437.  
  438.             $theExpression += "float $r = " + $shader + ".colorR;\n";
  439.             $theExpression += "float $g = " + $shader + ".colorG;\n";
  440.             $theExpression += "float $b = " + $shader + ".colorB;\n";
  441.  
  442.             $theExpression += "vector $rgb = <<$r, $g, $b>>;\n";
  443.             $theExpression += "vector $hsv = rgb_to_hsv($rgb);\n";
  444.             $theExpression += "$hsv = <<$hsv.x, $hsv.y, " + $incan + ">>;\n";
  445.             $theExpression += "$rgb = hsv_to_rgb($hsv);\n";
  446.  
  447.             $theExpression += $shader + ".incandescenceR = $rgb.x;\n";
  448.             $theExpression += $shader + ".incandescenceG = $rgb.y;\n";
  449.             $theExpression += $shader + ".incandescenceB = $rgb.z;\n\n";
  450.         }
  451.  
  452.         expression -n ($fireworksName + "SparksShadersExpr") -s $theExpression;
  453.     }
  454. }
  455.  
  456.  
  457. //  ================ connectFireworksAttributes ================
  458. //
  459. //  SYNOPSIS
  460. //      Connect the attributes on the fireworks group to the appropriate
  461. //        attributes in the various objects that make up the fireworks.
  462. //
  463. //    ARGUMENTS
  464. //        string $fireworksName    -- base fireworks name
  465. //        string $fireworksGroup   -- the top level fireworks group, which has the
  466. //                                    attributes the other objects' attributes are
  467. //                                    being connect to
  468. //        string $rocketsShape     -- the rockets particle
  469. //        string rocketTrailShape  -- the rocket trails particle
  470. //        string $burstSparksShape -- the burst sparks particle
  471. //        string $rocketTrailEmitter -- emitter of the rocket trails
  472. //
  473. //    RETURN
  474. //        None
  475. //
  476. proc connectFireworksAttributes(string $fireworksName,
  477.                                     string $fireworksGroup,
  478.                                     string $rocketsShape, 
  479.                                     string $rocketTrailShape, 
  480.                                     string $burstSparksShape,
  481.                                     string $rocketTrailEmitter)
  482. {
  483.     if( `licenseCheck -type complete` == 0 )
  484.     {
  485.         warning("You are not licensed to use the Fireworks Effect.");
  486.         return;
  487.     }
  488.  
  489.     global string $gFwSparksShaders[];
  490.     global string $gFwTrailShaders[];
  491.  
  492.     // ROCKETS ATTRIBUTES
  493.     //
  494.     connectAttr 
  495.         ($fireworksGroup + ".maxBurstSpeed")  
  496.         ($rocketsShape + ".maxBurstSpeed");
  497.  
  498.     connectAttr 
  499.         ($fireworksGroup + ".minSparksCount")  
  500.         ($rocketsShape + ".minSparksCount");
  501.  
  502.     connectAttr 
  503.         ($fireworksGroup + ".maxSparksCount")  
  504.         ($rocketsShape + ".maxSparksCount");
  505.  
  506.     connectAttr 
  507.         ($fireworksGroup + ".sparksColorSpread")  
  508.         ($rocketsShape + ".sparksColorSpread");
  509.  
  510.     connectAttr 
  511.         ($fireworksGroup + ".rocketGravity") 
  512.         ($rocketsShape + ".rocketGravity");
  513.  
  514.     connectAttr 
  515.         ($fireworksGroup + ".showAllBurstPositions") 
  516.         ($rocketsShape + ".showAllBurstPositions");
  517.  
  518.     connectAttr 
  519.         ($fireworksGroup + ".showAllLaunchPositions") 
  520.         ($rocketsShape + ".showAllLaunchPositions");
  521.  
  522.     connectAttr 
  523.         ($fireworksGroup + ".displayGeometry")  
  524.         ($rocketsShape + ".displayGeometry");
  525.  
  526.     // ROCKET TRAIL EMITTER ATTRIBUTES
  527.     //
  528.     connectAttr 
  529.         ($fireworksGroup + ".trailEmitRate")  
  530.         ($rocketTrailEmitter + ".rate");
  531.     connectAttr 
  532.         ($fireworksGroup + ".trailEmitSpeed")  
  533.         ($rocketTrailEmitter + ".speed");
  534.     connectAttr 
  535.         ($fireworksGroup + ".trailEmitSpread")  
  536.         ($rocketTrailEmitter + ".spread");
  537.  
  538.     // ROCKET TRAIL ATTRIBUTES
  539.     //
  540.     for ($i = 0; $i < size($gFwTrailShaders); $i++)
  541.     {
  542.         connectAttr 
  543.             ($fireworksGroup + ".trailGlow")  
  544.             ($gFwTrailShaders[$i] + ".glowIntensity");
  545.     }
  546.     connectAttr 
  547.         ($fireworksGroup + ".trailMinTailSize")  
  548.         ($rocketTrailShape + ".minTailSize");
  549.  
  550.     connectAttr 
  551.         ($fireworksGroup + ".trailMaxTailSize")  
  552.         ($rocketTrailShape + ".maxTailSize");
  553.  
  554.  
  555.     // BURST SPARKS ATTRIBUTES
  556.     //
  557.     connectAttr 
  558.         ($fireworksGroup + ".sparksMinTailSize")  
  559.         ($burstSparksShape + ".minTailSize");
  560.  
  561.     connectAttr 
  562.         ($fireworksGroup + ".sparksMaxTailSize")  
  563.         ($burstSparksShape + ".maxTailSize");
  564.  
  565.     for ($i = 0; $i < size($gFwSparksShaders); $i++)
  566.     {
  567.         connectAttr 
  568.             ($fireworksGroup + ".sparksGlow")  
  569.             ($gFwSparksShaders[$i] + ".glowIntensity");
  570.     }
  571.  
  572.  
  573.  
  574.     // Create the expression for the incandescence of the trail and sparks
  575.     // shaders.
  576.     //
  577.     createShaderIncandescenceExpressions($fireworksName, $fireworksGroup);
  578. }
  579.  
  580.  
  581. // ************************************************************************ //
  582. //                                                                          //
  583. //                           ROCKETS PROCEDURES                             //
  584. //                                                                          //
  585. // ************************************************************************ //
  586.  
  587.  
  588. //  ================ createRocketsExpression ================
  589. //
  590. //  SYNOPSIS
  591. //        Create the creation expression that sets rocket flight behavior.
  592. //
  593. //      Create the runtime expression that has two fuctions:
  594. //
  595. //        1. Writes the expression for the given rocket particle shape to control
  596. //        its flight.  The expression takes into account the launch and burst times
  597. //        and positions, and the direction and magnitude of the specified gravity
  598. //        field.  
  599. //
  600. //      2. Creates the fireworks bursts (sparks) through calling the emit action.  
  601. //        This expression is on the rocket particle from which the burst will
  602. //        be launched.
  603. //        For each burst, the expression creates a random number of particles
  604. //        between minSparksCount and maxSparksCount.  It creates a random set
  605. //        of positions, velocities and colors for the particles in the burst.
  606. //
  607. //    ARGUMENTS
  608. //        string $rocketShape          -- the rocket particles shape
  609. //        string $burstSparksShape     -- the burst sparks shape
  610. //        string $burstSparksInstancer -- the instancer (w/cones) for the burst sparks
  611. //        string $rocketTrailEmitter   -- emitter of the rocket trails
  612. //        string $grav                 -- gravity field influencing rockets
  613. //
  614. //    RETURN
  615. //        None
  616. //
  617. proc createRocketsExpression(string $rocketsShape,
  618.                                 string $burstSparksShape, 
  619.                                 string $burstSparksInstancer,
  620.                                 string $rocketTrailEmitter,
  621.                                 string $grav )
  622. {
  623.     string $ratePPName = $rocketTrailEmitter + "RatePP";
  624.  
  625.     // CREATION:
  626.     //
  627.     string $cExpString = 
  628.         "// All launch particles live starting at time zero. \n"
  629.         + "// But make particle invisible until it's launched.\n"
  630.         + "//\n"
  631.         + "opacityPP = 0.0;\n"
  632.         + "\n"
  633.         + "// Compute and store necessary in-flight velocity.\n"
  634.         + "//\n"
  635.         + "float $totalFlightTimeFrames = burstFramePP - launchFramePP - 1;\n"
  636.         + "float $totalFlightTimeSeconds = $totalFlightTimeFrames;\n"
  637.         + "\n"
  638.         + "string $timeUnit = `currentUnit -q -time`;\n"
  639.         + "if ($timeUnit == \"film\")\n"
  640.         + "    $totalFlightTimeSeconds = $totalFlightTimeFrames/24;\n"
  641.         + "else if ($timeUnit == \"ntsc\")\n"
  642.         + "    $totalFlightTimeSeconds = $totalFlightTimeFrames/30;\n"
  643.         + "else if ($timeUnit == \"pal\")\n"
  644.         + "    $totalFlightTimeSeconds = $totalFlightTimeFrames/25;\n"
  645.         + "else if ($timeUnit == \"game\")\n"
  646.         + "    $totalFlightTimeSeconds = $totalFlightTimeFrames/15;\n"
  647.         + "else if ($timeUnit == \"show\")\n"
  648.         + "    $totalFlightTimeSeconds = $totalFlightTimeFrames/48;\n"
  649.         + "else if ($timeUnit == \"palf\")\n"
  650.         + "    $totalFlightTimeSeconds = $totalFlightTimeFrames/50;\n"
  651.         + "else if ($timeUnit == \"ntscf\")\n"
  652.         + "    $totalFlightTimeSeconds = $totalFlightTimeSeconds/60;\n"
  653.         + "vector $gravDir = << localGravX, localGravY, localGravZ >>;\n"
  654.         + "initialVelocity = (burstPositionPP - launchPositionPP) /  $totalFlightTimeSeconds "
  655.         + "- $gravDir * 0.5 * rocketGravity * $totalFlightTimeSeconds;\n"
  656.         + "\n"
  657.         + "// Initialize velocity to zero prior to launch. Set launched flag false.\n"
  658.         + "velocity = <<0,0,0>>;\n"
  659.         + "launched = 0;\n"
  660.         + "hasBurst = 0;\n"
  661.         + $ratePPName + " = 0;\n"
  662.         + "\n"
  663.         + "\tposition = launchPositionPP;\n"
  664.       ;
  665.  
  666.     dynExpression -c -s $cExpString $rocketsShape;
  667.  
  668.  
  669.     // RUNTIME:
  670.     //
  671.     string $rExpString = 
  672.           "// FLIGHT CONTROLS:\n"
  673.         + "//\n"
  674.         + "// Make rocket particles invisible until they are launched\n"
  675.         + "// and after they have burst.\n"
  676.         + "//\n"
  677.         + "if (frame >= launchFramePP &&  hasBurst == 0)\n"
  678.         + "\topacityPP = 1.0;\n"
  679.         + "\n"
  680.         + "//If not yet launched, keep at launch position\n"
  681.         + "//Give initial velocity at launch time or soonest chance after.\n"
  682.         + "//\n"
  683.         + "if (frame <= launchFramePP-1)\n"
  684.         + "{\n"
  685.         + "\tvelocity = initialVelocity;\n"
  686.         + "\tposition = launchPositionPP;\n"
  687.         + "}\n"
  688.         + "else\n"
  689.         + "if (0 == launched)\n"
  690.         + "{\n"
  691.         + "\tvelocity = initialVelocity;\n"
  692.         + "\tlaunched = 1;\n"
  693.         + "}\n"
  694.         + "// Before and after the burst frame, the rocket should not emit.\n"
  695.         + "//\n"
  696.         + "if (frame < launchFramePP || hasBurst == 1)\n"
  697.         + "\t" + $ratePPName + " = 0;\n"
  698.         + "else\n"
  699.         + "\t" + $ratePPName + " = " + $rocketTrailEmitter + ".rate;\n\n"
  700.         + "//\n"
  701.         + "// BURST CONTROLS:\n"
  702.         + "//\n"
  703.         + "vector $rocketPosition = worldPosition;\n"
  704.         + "vector $rocketVelocity = velocity;\n"
  705.         + "\n"
  706.         + "// If the rocket particle is above the minimum explode height or the\n"
  707.         + "// rocket particle is falling, then make the fireworks burst,\n"
  708.         + "// if it hasn't already.\n"
  709.         + "//\n"
  710.         + "if( frame >= burstFramePP-1 && hasBurst == 0)\n"
  711.         + "{\n"
  712.         + "    float $fmin = minSparksCount;\n"
  713.         + "    float $fmax = maxSparksCount;\n"
  714.         + "    if( $fmax < $fmin )\n"
  715.         + "        $fmax = $fmin;\n"
  716.         + "\n"    
  717.         + "    // Determine a random number of sparks/particles in this burst.\n"
  718.         + "    //\n"
  719.         + "    int $emitCount = rand($fmin,$fmax);\n"
  720.         + "\n"
  721.         + "       vector $pattern[];\n"
  722.         + "       clear($pattern);\n"
  723.         + "\n"
  724.         + "    // Fill the pattern array with random vectors, producing\n"
  725.         + "    // a random, spherical explosion.\n"
  726.         + "    //\n"
  727.         + "    for( $i = 0; $i < $emitCount; $i ++ )\n"
  728.         + "    {\n"
  729.         + "        $pattern[$i] = sphrand(1);\n"
  730.         + "    }\n"
  731.         + "\n"
  732.         + "    // Set up the emit action for creating the burst sparks\n"
  733.         + "    //\n"
  734.         + "    // Emit into the Sparks particle object\n"
  735.         + "    //\n"
  736.         + "    string $cmd = \"emit -object " +  $burstSparksShape + "\";\n"
  737.         + "\n"
  738.         + "    // Set the position from which the emission occurs to be the \n"
  739.         + "    // position of the rocket.\n"
  740.         + "    //\n"
  741.         + "    for( $i = 0; $i < $emitCount; $i ++ )\n"
  742.         + "    {\n"
  743.         + "        $cmd += (\" -pos \"+$rocketPosition);\n"
  744.         + "    }\n"
  745.         + "\n"
  746.         + "    // Set the velocities of the spark particles.\n"
  747.         + "    //\n"
  748.         + "    $cmd += \" -at velocity\";\n"
  749.         + "    for( $i = 0; $i < $emitCount; $i ++ )\n"
  750.         + "    {\n"
  751.         + "        //\n"
  752.         + "        vector $nv = $pattern[$i];\n"
  753.         + "        $nv = $nv * maxBurstSpeed + $rocketVelocity;\n"
  754.         + "\n"
  755.         + "        $cmd += (\" -vv \"+$nv);\n"
  756.         + "    }\n"
  757.         + "\n"
  758.         + "    // Set the color index for the sparks.\n"
  759.         + "    //\n"
  760.         + "\n"
  761.         + "    // For each spark particle, choose randomly either the main color\n"
  762.         + "    // of the burst or one of its adjacent colors -- to get\n"
  763.         + "    // color modulation in the burst.\n"
  764.         + "    //\n"
  765.         + "    // Number of colors to choose from.\n"
  766.         + "    //\n"
  767.         + "    float $indexCount = " + $burstSparksInstancer + ".hierarchyCount;\n"
  768.         + "\n"
  769.         + "    int $startIndex = ($indexCount + burstColorIndex - sparksColorSpread) % $indexCount;\n"
  770.         + "    int $endIndex = ($startIndex + sparksColorSpread * 2) % $indexCount;\n"
  771.         + "\n"
  772.         + "    float $red[]; float $green[]; float $blue[];\n"
  773.         + "    clear($red); clear($green); clear($blue);\n"
  774.         + "\n"
  775.         + "    int $c;\n"
  776.         + "    for( $c = 0; $c < sparksColorSpread * 2 + 1; $c ++ )\n"
  777.         + "    {\n"
  778.         + "           float $getColor[] = `getAttr " + $rocketsShape + ".fireworksColors[($startIndex+$c)%$indexCount]`;\n"
  779.         + "           $red[$c] = $getColor[0];\n"
  780.         + "           $green[$c] = $getColor[1];\n"
  781.         + "           $blue[$c] = $getColor[2];\n"
  782.         + "    }\n"
  783.         + "\n"
  784.         + "    for ($i = 0; $i < $emitCount; $i++)\n"
  785.         + "    {\n"
  786.         + "        int $index = rand(sparksColorSpread * 2 + 1);\n"
  787.         + "        $cmd += \" -at sparkColorIndex \";\n"
  788.         + "        $cmd += \"  -fv \" + (($startIndex + $index)%$indexCount);\n"
  789.         + "        $cmd += \" -at rgbPP \";\n"
  790.         + "        $cmd += \"  -vv \"+$red[$index]+\" \"+$green[$index]+\" \"+$blue[$index];\n"
  791.         + "    }\n"
  792.         + "\n"
  793.         + "    // Now issue the emit command\n"
  794.         + "    //\n"
  795.         + "    eval($cmd);\n"
  796.         + "\n "
  797.         + "    hasBurst = 1;\n"
  798.         + "}\n"
  799.     ;
  800.  
  801.     dynExpression -r -s $rExpString $rocketsShape;
  802.  
  803.  
  804.     // Set expressions to execute after dynamics.  The runtime expression
  805.     // requires this to keep particles in place prior to launch time.
  806.     // Then lock the attribute because if it is changed, it will affect
  807.     // the rocket flight.
  808.     //
  809.     setAttr ($rocketsShape + ".expressionsAfterDynamics") 1;
  810.     setAttr -lock true ($rocketsShape+".expressionsAfterDynamics");
  811. }
  812.  
  813. //  ================ addAndConnectRocketAttributes ================
  814. //
  815. //  SYNOPSIS
  816. //
  817. //        Adds attributes to control the rockets flight to the rockets particle
  818. //        shape, whose name is passed in.  Assumes that proper error checking
  819. //        has been done and that this is a valid particle shape.
  820. //        Also assumes it is a newly created particle shape so it won't yet have
  821. //        any of these attributes.
  822. //        $gravity is the name of the gravity field on the rockets.
  823. //        This routine connects it to control parameters in the particle shape.
  824. //        This routine assumes the names are valid.
  825. //
  826. //    ARGUMENTS
  827. //        string $fireworksGroup -- the top level group for the fireworks
  828. //        string $rocketShape    -- the rocket particles shape
  829. //        string $gravity        -- the rockets' gravity field
  830. //
  831. //    RETURN
  832. //        None
  833. //
  834. proc addAndConnectRocketAttributes( string $fireworksGroup,
  835.                                     string $rocketsShape, 
  836.                                     string $gravity )
  837. {
  838.     // Launch and burst locations and frames
  839.     //
  840.     addAttr -ln launchPositionPP0 -dt vectorArray  $rocketsShape;
  841.     addAttr -ln launchPositionPP -dt vectorArray  $rocketsShape;
  842.  
  843.     addAttr -ln burstPositionPP0 -dt vectorArray  $rocketsShape;
  844.     addAttr -ln burstPositionPP -dt vectorArray  $rocketsShape;
  845.  
  846.     addAttr -ln launchFramePP0 -dt doubleArray  $rocketsShape;
  847.     addAttr -ln launchFramePP -dt  doubleArray  $rocketsShape;
  848.  
  849.     addAttr -ln burstFramePP0 -dt  doubleArray  $rocketsShape;
  850.     addAttr -ln burstFramePP -dt   doubleArray  $rocketsShape;
  851.  
  852.     addAttr -ln opacityPP0 -dt  doubleArray  $rocketsShape;
  853.     addAttr -ln opacityPP -dt   doubleArray  $rocketsShape;
  854.  
  855.     addAttr -ln initialVelocity -dt vectorArray  $rocketsShape;
  856.     addAttr -ln launched -dt doubleArray  $rocketsShape;
  857.     addAttr -ln hasBurst -dt doubleArray  $rocketsShape;
  858.  
  859.     // Add an attribute to control display of sparks as particles or
  860.     // cones.  
  861.     //
  862.     addAttr -is true -ln "displayGeometry" -at bool -dv true $rocketsShape;
  863.     setAttr -e -keyable true ($rocketsShape+".displayGeometry");
  864.  
  865.     // spin control
  866.     //
  867.     addAttr -ln spinSpeed -at double  -dv 0.0 $rocketsShape;
  868.     addAttr -ln spinSpread -at double  -dv 0.0 $rocketsShape;
  869.     setAttr -e -keyable true ($rocketsShape+".spinSpeed");
  870.     setAttr -e -keyable true ($rocketsShape+".spinSpread");
  871.  
  872.     // Attribute to hold the color palette for the rocket burst instances
  873.     // so we can have ready access to it in the Attribute Editor.  And 
  874.     // another to hold a message connection between the palette color and the
  875.     // shader color for editing purposes.
  876.     //
  877.     addAttr -ln "fireworksColors" -dt double3 -multi $rocketsShape;
  878.     addAttr -ln "fireworksShaders" -at message -multi $rocketsShape;
  879.  
  880.     addAttr -ln "rocketTrailColors" -dt double3 -multi $rocketsShape;
  881.     addAttr -ln "rocketTrailShaders" -at message -multi $rocketsShape;
  882.  
  883.     // Used to index into the particle instancer to choose one of the color
  884.     // cones as the "main color" for the burst that comes from each rocket.
  885.     //
  886.     addAttr -ln "burstColorIndex" -dt doubleArray $rocketsShape;
  887.     addAttr -ln "burstColorIndex0" -dt doubleArray $rocketsShape;
  888.  
  889.     // Used to save the name of the procedure that created the color palette
  890.     // for the rocket sparks, so the user can reset to that original
  891.     // palette.
  892.     //
  893.     addAttr -ln "colorPaletteProc" -dt "string" $rocketsShape;
  894.     setAttr -e -keyable false ($rocketsShape + ".colorPaletteProc");
  895.  
  896.     // Attribute just to flag that this particle shape is a rocket shape for
  897.     // fireworks.  So we can put controls in the attribute for its fireworks
  898.     // attributes.  But it should not be visible or editable, so make it
  899.     // locked and unkeyable.
  900.     //
  901.     addAttr -ln "_fireworksRocket" -at message $rocketsShape;
  902.     setAttr -keyable false -lock on ($rocketsShape + "._fireworksRocket");
  903.  
  904.     // Message attributes:  
  905.     
  906.     // These will be connected to the launch and burst position locators,
  907.     // so we can find the locators in the Attribute Editor.
  908.     //
  909.     addAttr -ln "launchPositionManipMessage" -at message $rocketsShape;
  910.     setAttr -keyable false ($rocketsShape + ".launchPositionManipMessage");
  911.     addAttr -ln "burstPositionManipMessage" -at message $rocketsShape;
  912.     setAttr -keyable false ($rocketsShape + ".burstPositionManipMessage");
  913.  
  914.     // Also connect a message to the fireworksGroup so we can find it in
  915.     // the attribute editor for editing sparksColorSpread and visibility.
  916.     //
  917.     addAttr -ln "rocketsMessage" -at message $rocketsShape;
  918.     setAttr -keyable false ($rocketsShape + ".rocketsMessage");
  919.     connectAttr ($fireworksGroup + ".message")
  920.                 ($rocketsShape + ".rocketsMessage");
  921.  
  922.     // Add attributes that will be used in creating and controlling the
  923.     // sparks in the rocket's fireworks burst.
  924.     //
  925.     addAttr -is true -ln "minSparksCount" 
  926.             -at "float" -min 0 -dv 100     
  927.             $rocketsShape;
  928.     addAttr -is true -ln "maxSparksCount" 
  929.             -at "float" -min 0 -dv 200 
  930.             $rocketsShape;
  931.     addAttr -is true -ln "sparksColorSpread" 
  932.             -at "float" -min 0 -dv 3 
  933.             $rocketsShape;
  934.     addAttr -is true -ln "maxBurstSpeed" 
  935.             -at "float" -min 0 -dv 20 
  936.             $rocketsShape;
  937.  
  938.     setAttr -keyable on ($rocketsShape + ".maxBurstSpeed");
  939.     setAttr -keyable on ($rocketsShape + ".minSparksCount");
  940.     setAttr -keyable on ($rocketsShape + ".maxSparksCount");
  941.     setAttr -keyable on ($rocketsShape + ".sparksColorSpread");
  942.  
  943.     // gravity control, connected to gravity shape
  944.     //
  945.     addAttr -ln rocketGravity -at double -dv 9.8 $rocketsShape;
  946.     setAttr -e -keyable true ($rocketsShape+".rocketGravity");
  947.     connectAttr ($rocketsShape + ".rocketGravity") ($gravity + ".magnitude");
  948.  
  949.     // toggle for viewing mode
  950.     //
  951.     addAttr -ln showAllBurstPositions -at bool  $rocketsShape;
  952.     setAttr -e -keyable false ($rocketsShape+".showAllBurstPositions");
  953.  
  954.     addAttr -ln showAllLaunchPositions -at bool  $rocketsShape;
  955.     setAttr -e -keyable false ($rocketsShape+".showAllLaunchPositions");
  956.  
  957. }
  958.  
  959.  
  960. //  ================ createRocketParticles ================
  961. //
  962. //  SYNOPSIS
  963. //        Given the name of an existing particle shape $rocketsShape, adds to it
  964. //        $numRockets particles.
  965. //        Each particle is given a random burst position inside a box centered
  966. //        at $burstAreaCenter, each of whose edges is $burstAreaExtents.  Each
  967. //        particle's launch position is $launchPosition (all the same).
  968. //        Launch times for each particle are generated as a function of
  969. //        $firstLaunchFrame and $launchRate (number per frame).
  970. //        The burst time for each particle is equal to its launch time plus the
  971. //        fixed value $flightTimeInFrames.
  972. //
  973. //        Assumes the $rocketsShape is valid and is a particle shape. Assumes
  974. //        addAndConnectRocketAttributes has already been called to add the
  975. //        necessary attributes.  
  976. //
  977. //        This routine is called when first making the effect or when
  978. //        adding more particles to an existing effect.
  979. //
  980. //
  981. //    ARGUMENTS
  982. //        string $rocketShape      -- the rocket particles shape
  983. //        int    $numRockets       -- number of particles/rockets to create 
  984. //        vector $launchPos        -- launch position
  985. //        vector $burstAreaCenter  -- center of burst area
  986. //        vector $burstAreaExtents -- extents of burst area
  987. //        float  $firstLaunchFrame -- frame for first launch
  988. //        float  $launchRate       -- launch rate (in frames)
  989. //        float  $minFlightTimeInFrames
  990. //        float  $maxFlightTimeInFrames
  991. //
  992. //    RETURN
  993. //        None
  994. //
  995. proc createRocketParticles( string $rocketsShape, 
  996.                                 int $numRockets, 
  997.                                 vector $launchPos, 
  998.                                 vector $burstAreaCenter, 
  999.                                   vector $burstAreaExtents,
  1000.                                   float $firstLaunchFrame, 
  1001.                                 float $launchRate, 
  1002.                                 float $minFlightTimeInFrames,
  1003.                                   float $maxFlightTimeInFrames )
  1004. {
  1005.     if (($launchRate == 0) || ($numRockets == 0)) return;  
  1006.  
  1007.     int $i;
  1008.     
  1009.     // Data to generate random burst positions
  1010.     //
  1011.     vector $llc;
  1012.     $llc = $burstAreaCenter -  $burstAreaExtents * 0.5;
  1013.  
  1014.     // Data to generate launch frames
  1015.     //
  1016.     float $interval = 1 / $launchRate;    
  1017.  
  1018.     // If start time of animation is negative, make the particle shape start there.
  1019.     //
  1020.     //
  1021.     float $animStart = `playbackOptions -q -min`;
  1022.     if ($animStart < `getAttr ($rocketsShape + ".startFrame")`)
  1023.     {
  1024.         setAttr ($rocketsShape + ".startFrame") $animStart;
  1025.     }
  1026.  
  1027.     // Set a random seed so multiple fireworks objects will not all have
  1028.     // the same rocket burst positions.
  1029.     //
  1030.     seed(int(timerX()));
  1031.  
  1032.     // Emit commands to add the particles
  1033.     //
  1034.     for ($i = 0; $i < $numRockets; $i++)
  1035.     {
  1036.         string $cmd;
  1037.  
  1038.         // Generate random burst position
  1039.         //
  1040.         vector $pos;
  1041.         float $x = rand( $burstAreaExtents.x );
  1042.          float $y = rand( $burstAreaExtents.y );
  1043.          float $z = rand( $burstAreaExtents.z );
  1044.         $pos = $llc + << $x, $y, $z >>;
  1045.  
  1046.         float $flightTimeInFrames = rand( $minFlightTimeInFrames, $maxFlightTimeInFrames );
  1047.  
  1048.         // Lay out launch times at prescribed interval.
  1049.         //
  1050.         int $launchFrame = $firstLaunchFrame + $i * $interval;
  1051.         int $burstFrame  = $launchFrame + $flightTimeInFrames;
  1052.  
  1053.         $cmd = "emit  -object " + $rocketsShape;
  1054.           $cmd += 
  1055.             " -position " + $launchPos.x + " " + $launchPos.y + " "+ $launchPos.z;
  1056.           $cmd += " -at opacityPP0 -fv 0.0 ";
  1057.           $cmd += " -at opacityPP -fv 0.0 ";
  1058.         $cmd += " -at launchPositionPP -vv " + $launchPos.x + " " + $launchPos.y + " "+ $launchPos.z;
  1059.           $cmd += " -at launchPositionPP0 -vv " + $launchPos.x + " " + $launchPos.y + " "+ $launchPos.z;
  1060.           $cmd += " -at burstPositionPP -vv " + $pos.x + " " + $pos.y + " "+ $pos.z;
  1061.           $cmd += " -at burstPositionPP0 -vv " + $pos.x + " " + $pos.y + " "+ $pos.z;
  1062.         $cmd += " -at launchFramePP -fv " + $launchFrame;
  1063.         $cmd += " -at launchFramePP0 -fv " + $launchFrame;
  1064.         $cmd += " -at burstFramePP -fv " + $burstFrame; 
  1065.         $cmd += " -at burstFramePP0 -fv " + $burstFrame;
  1066.         
  1067.         // Execute the command
  1068.         //
  1069.         eval( $cmd );
  1070.     }
  1071.  
  1072.     // save current positions to initial state
  1073.     //
  1074.     saveInitialState $rocketsShape;
  1075. }
  1076.  
  1077.  
  1078. //  ================ hideAndLockRocketAttributes ================
  1079. //
  1080. //  SYNOPSIS
  1081. //        Hides and locks standard attributes which we do not wish to display
  1082. //        in the firework effect.  This includes the emission-related and render
  1083. //        type attributes.
  1084. //
  1085. //
  1086. //    ARGUMENTS
  1087. //        string $rocketShape -- the rocket particles shape
  1088. //
  1089. //    RETURN
  1090. //        None
  1091. //
  1092. proc hideAndLockRocketAttributes( string $rocketShape )
  1093. {
  1094.     setAttr -keyable false ($rocketShape+".inheritFactor");
  1095.     setAttr -keyable false ($rocketShape+".emissionInWorld");
  1096.     setAttr -keyable false ($rocketShape+".maxCount");
  1097.     setAttr -keyable false ($rocketShape+".inputGeometrySpace");
  1098.     setAttr -keyable false ($rocketShape+".enforceCountFromHistory");
  1099.     setAttr -keyable false ($rocketShape+".targetGeometrySpace");
  1100.     setAttr -keyable false ($rocketShape+".goalSmoothness");
  1101.     setAttr -keyable false ($rocketShape+".levelOfDetail");
  1102.     setAttr -keyable false ($rocketShape+".expressionsAfterDynamics");
  1103.     setAttr -keyable false ($rocketShape+".dynamicsWeight");
  1104.     setAttr -keyable false ($rocketShape+".forcesInWorld");
  1105.     setAttr -keyable false ($rocketShape+".traceDepth");
  1106.  
  1107.     setAttr -lock true ($rocketShape+".forcesInWorld");
  1108.     setAttr -lock true ($rocketShape+".dynamicsWeight");
  1109.     setAttr -lock true ($rocketShape+".maxCount");
  1110.     
  1111. }
  1112.  
  1113.  
  1114. //  ================ createRocketPositionParticles ================
  1115. //
  1116. //  SYNOPSIS
  1117. //        Create two "shadow" particle objects, each with "numRockets" number
  1118. //        of particles.  One will be turned on/off to show/hide the launch
  1119. //        positions of the "real" rockets, and the other will be turned on/off
  1120. //        to show/hide the burst positions of the "real" rockets.
  1121. //
  1122. //    ARGUMENTS
  1123. //        string $fireworksName -- base name of the fireworks
  1124. //        int    $numRockets    -- number of particles to create
  1125. //        string $rocketShape   -- the rocket particles shape
  1126. //
  1127. //    RETURN
  1128. //        None
  1129. //
  1130. proc string createRocketPositionParticles(string $fireworksName,
  1131.                                             int $numRockets, 
  1132.                                             string $rocketsShape)
  1133. {
  1134.     // Build the command line, creating as many particles in each object
  1135.     // as there are rockets.
  1136.     //
  1137.  
  1138.     string $rocketsParents[] = `listRelatives -parent $rocketsShape`;
  1139.     string $rockets = $rocketsParents[0];
  1140.  
  1141.     float $launchPositions[];
  1142.     float $burstPositions[];
  1143.  
  1144.     float $launchPos[];
  1145.     float $burstPos[];
  1146.  
  1147.     string $launchCmd = "particle -n " + $fireworksName + "LaunchPositions ";
  1148.     string $burstCmd = "particle -n " + $fireworksName + "BurstPositions ";
  1149.     
  1150.     // Get the launch and burst positions of the rocket and create the
  1151.     // position particles at the same place.
  1152.     //
  1153.     for ($i = 0; $i < $numRockets; $i++)
  1154.     {
  1155.         $launchPos = 
  1156.             `particle -attribute launchPositionPP -id $i -q $rocketsShape`;
  1157.  
  1158.         $burstPos = 
  1159.             `particle -attribute burstPositionPP -id $i -q $rocketsShape`;
  1160.  
  1161.         $launchCmd += "-p " + $launchPos[0] + " " 
  1162.                             + $launchPos[1] + " " 
  1163.                             + $launchPos[2] + " ";
  1164.  
  1165.         $burstCmd += "-p "  + $burstPos[0] + " " 
  1166.                             + $burstPos[1] + " " 
  1167.                             + $burstPos[2] + " ";
  1168.  
  1169.     }
  1170.  
  1171.     string $launchPosName[] = eval($launchCmd);
  1172.     string $burstPosName[]  = eval ($burstCmd);
  1173.     string $launchPosParticle = $launchPosName[0];
  1174.     string $launchShape = $launchPosName[1];
  1175.     string $burstPosParticle = $burstPosName[0];
  1176.     string $burstShape = $burstPosName[1];
  1177.  
  1178.     setAttr ($launchShape + ".particleRenderType") 2;
  1179.     setAttr ($burstShape + ".particleRenderType") 2;
  1180.  
  1181.     // Template the particles so they can't be selected/edited.
  1182.     //
  1183.     setAttr ($launchShape + ".template") 1;
  1184.     setAttr ($burstShape + ".template") 1;
  1185.  
  1186.     // Connect their display to the rocketsShape.showAllBurstPositions and
  1187.     // showAllLaunchPositions.
  1188.     //
  1189.     connectAttr
  1190.         ($rocketsShape + ".showAllBurstPositions")
  1191.         ($burstShape + ".visibility");
  1192.  
  1193.     connectAttr
  1194.         ($rocketsShape + ".showAllLaunchPositions")
  1195.         ($launchShape + ".visibility");
  1196.  
  1197.     parent $launchPosParticle $rockets;
  1198.     parent $burstPosParticle $rockets;
  1199.  
  1200.     // Create an expression that sets the position of each particle to be
  1201.     // that of the corresponding rocket id's position (burst and launch);
  1202.     // We need the expression because the launch and burst positions of each
  1203.     // particle can be edited after creation.
  1204.     //
  1205.     string $launchExpr = "position = " + $rocketsShape + ".launchPositionPP0;";
  1206.     string $burstExpr = "position = " + $rocketsShape + ".burstPositionPP0";
  1207.  
  1208.     dynExpression -c -s $launchExpr $launchShape;
  1209.     dynExpression -r -s $launchExpr $launchShape;
  1210.  
  1211.     dynExpression -c -s $burstExpr $burstShape;
  1212.     dynExpression -r -s $burstExpr $burstShape;
  1213.  
  1214.     setAttr -lock true ($launchPosParticle+".tx");
  1215.     setAttr -lock true ($launchPosParticle+".ty");
  1216.     setAttr -lock true ($launchPosParticle+".tz");
  1217.     setAttr -lock true ($launchPosParticle+".rx");
  1218.     setAttr -lock true ($launchPosParticle+".ry");
  1219.     setAttr -lock true ($launchPosParticle+".rz");
  1220.     setAttr -lock true ($launchPosParticle+".sx");
  1221.     setAttr -lock true ($launchPosParticle+".sy");
  1222.     setAttr -lock true ($launchPosParticle+".sz");
  1223.  
  1224.     setAttr -lock true ($burstPosParticle+".tx");
  1225.     setAttr -lock true ($burstPosParticle+".ty");
  1226.     setAttr -lock true ($burstPosParticle+".tz");
  1227.     setAttr -lock true ($burstPosParticle+".rx");
  1228.     setAttr -lock true ($burstPosParticle+".ry");
  1229.     setAttr -lock true ($burstPosParticle+".rz");
  1230.     setAttr -lock true ($burstPosParticle+".sx");
  1231.     setAttr -lock true ($burstPosParticle+".sy");
  1232.     setAttr -lock true ($burstPosParticle+".sz");
  1233.  
  1234.     return $launchShape;
  1235.  
  1236. }
  1237.  
  1238.     
  1239. //  ================ createRocketTrailsEmitter ================
  1240. //
  1241. //  SYNOPSIS
  1242. //        Create the emitter that will emit the rocket sparks trails and
  1243. //        add it to the rocket object.
  1244. //
  1245. //
  1246. //    ARGUMENTS
  1247. //        string $fireworksName -- base fireworks name
  1248. //        string $rocketsShape  -- the rockets particle shape
  1249. //
  1250. //    RETURN
  1251. //        Name of rocket trails emitter
  1252. //
  1253. proc string createRocketTrailsEmitter(string $fireworksName, 
  1254.                                         string $rocketsShape)
  1255. {
  1256.  
  1257.     // Create the rocket's emitter, which emits into the trail sparks particle.
  1258.     //
  1259.     string $result[] = `emitter -pos 0 0 0
  1260.                         -n ($fireworksName + "RocketsEmitter")
  1261.                         -type direction -dx 0.0 -dy 1.0 -dz 0.0
  1262.                         -rate 150 -spread 0.5 -speed 0.5`;
  1263.  
  1264.     string $rocketTrailEmitter = $result[0];
  1265.  
  1266.     // Create an expression to somewhat randomize the emission, so the
  1267.     // sparks will not just come out in a straight line.
  1268.     // CAROL:  ROB will change this to do a spin rather than a spread.
  1269.     //
  1270.     // expression 
  1271.     //     -n ($rocketTrailEmitter + "Expr")
  1272.     //     -s ("directionX = rand(.1, .3);\n"
  1273.     //     +   "directionZ = rand(.1, .3);\n"
  1274.     //     +   "directionY = rand(.8, 1);")
  1275.      //     -o $rocketTrailEmitter;
  1276.  
  1277.     // Add the emitter to the Fireworks particle.
  1278.     //
  1279.     addDynamic $rocketTrailEmitter $rocketsShape;
  1280.  
  1281.     // Add rate PP.
  1282.     //
  1283.     addPP -atr "rate" $rocketTrailEmitter;
  1284.  
  1285.     // Hide the emitter.
  1286.     //
  1287.     hide $rocketTrailEmitter;
  1288.  
  1289.     return $rocketTrailEmitter;
  1290. }
  1291.  
  1292.  
  1293. //
  1294. //  ================ setBurstColorIndices ================
  1295. //
  1296. //  SYNOPSIS
  1297. //        Set the color for each of the burst sparks in the burst sparks particle
  1298. //        sparkColorIndex per particle attribute.  The color is determined randomly.
  1299. //
  1300. //    ARGUMENTS
  1301. //        string $rocketsShape         -- name of the rockets shape
  1302. //        int $numConeColors           -- number of cone/color instances
  1303. //
  1304. //    RETURN
  1305. //        None
  1306. //
  1307. proc setBurstColorIndices(string $rocketsShape, int $numConeColors)
  1308. {
  1309.     // Get the number of rockets, as that gives us the number of bursts, i.e.
  1310.     // color sets to create.
  1311.     //
  1312.     int $numRockets = `particle -q -count $rocketsShape`;
  1313.  
  1314.     // For each rocket, randomly pick one of the numbers in the color set 
  1315.     // and assign it to the next particle in the rocket.
  1316.     //
  1317.     for ($i = 0; $i < $numRockets; $i++)
  1318.     {
  1319.         int $index = rand($numConeColors);
  1320.  
  1321.         // Set this color index into the burstShape's burstColorIndex
  1322.         //
  1323.         particle -e
  1324.             -attribute burstColorIndex -order $i -fv $index $rocketsShape;
  1325.     }
  1326. }
  1327.  
  1328.  
  1329. //  ================ makeGravityLocatorsForRockets ================
  1330. //
  1331. //  SYNOPSIS
  1332. //
  1333. //          Creates four locators used to compute gravity direction in local space
  1334. //          of the particlce shape.  Two of the locators represent the head and tail
  1335. //          of the gravity direction (normalized) and two of them represent the
  1336. //          corresponding points in the particle object's local space.
  1337. //
  1338. //        $gravity is the name of the gravity field on the rockets.
  1339. //        This routine assumes the names are valid.
  1340. //
  1341. //    ARGUMENTS
  1342. //        string $rockets     -- the rocket particles transform
  1343. //        string $rocketShape -- the rocket particles shape
  1344. //        string $gravity     -- the rockets' gravity field
  1345. //
  1346. //    RETURN
  1347. //        None
  1348. //
  1349. proc string [] makeGravityLocatorsForRockets( string $rockets,
  1350.                                                 string $rocketsShape, 
  1351.                                                 string $gravity )
  1352. {
  1353.     string $rocketsParents[] = `listRelatives -parent $rocketsShape`;
  1354.     string $rockets = $rocketsParents[0];
  1355.  
  1356.     // Make and hide locators for the head and tail of the gravity, in world space.
  1357.     //
  1358.     createPrimitive nullObject;
  1359.     string $gravHeadLoc = `rename ($gravity+"HeadLocator")`;
  1360.     createPrimitive nullObject;
  1361.     string $gravTailLoc = `rename ($gravity+"TailLocator")`;
  1362.     hide  $gravHeadLoc;
  1363.     hide  $gravTailLoc;
  1364.  
  1365.     // Lock their transforms and turn off inherit transform. 
  1366.     // Transforms do not affect gravity direction.
  1367.     //
  1368.     setAttr -lock true ($gravHeadLoc+".rx");
  1369.     setAttr -lock true ($gravHeadLoc+".ry");
  1370.     setAttr -lock true ($gravHeadLoc+".rz");
  1371.     setAttr -lock true ($gravHeadLoc+".sx");
  1372.     setAttr -lock true ($gravHeadLoc+".sy");
  1373.     setAttr -lock true ($gravHeadLoc+".sz");
  1374.     setAttr ($gravHeadLoc+".inheritsTransform") 0;
  1375.  
  1376.     setAttr -lock true ($gravTailLoc+".rx");
  1377.     setAttr -lock true ($gravTailLoc+".ry");
  1378.     setAttr -lock true ($gravTailLoc+".rz");
  1379.     setAttr -lock true ($gravTailLoc+".sx");
  1380.     setAttr -lock true ($gravTailLoc+".sy");
  1381.     setAttr -lock true ($gravTailLoc+".sz");
  1382.     setAttr ($gravTailLoc+".inheritsTransform") 0;
  1383.  
  1384.     // Expression sets the position of the "head" locator to be the
  1385.     // (unit) direction of the gravity.  Important that this is a unit
  1386.     // vector, i.e., normalized.
  1387.     //
  1388.     string $expString = "\r\n// Set the locator's translate to be the normalized direction of the gravity\r\n//\r\n\r\n" +
  1389.                 "float $x = " + $gravity + ".directionX;\r\n" +
  1390.                 "float $y = " + $gravity + ".directionY;\r\n" +
  1391.                 "float $z = " + $gravity + ".directionZ;\r\n\r\n" +
  1392.                 "vector $uDir = unit( << $x,  $y, $z >> );\r\n" +
  1393.                 $gravHeadLoc + ".translateX = $uDir.x;\r\n" +
  1394.                 $gravHeadLoc + ".translateY = $uDir.y;\r\n" +
  1395.                 $gravHeadLoc + ".translateZ = $uDir.z;\r"  ;
  1396.  
  1397.     expression -n ($gravHeadLoc + "Expr") -s $expString -o $gravHeadLoc; 
  1398.  
  1399.     // Make and hide two corresponding locators and parent them to the particle object.
  1400.     // (Leave inherit transform ON, but lock transform attributes.)
  1401.     // Note that we use the particle shape name in the parent command because that's what
  1402.     // we have available.  The parent command interprets it correctly.
  1403.     //
  1404.     createPrimitive nullObject;
  1405.     string $partHeadLoc = `rename ($rockets+"HeadLocator")`;
  1406.     parent $partHeadLoc $rockets;
  1407.     createPrimitive nullObject;
  1408.     string $partTailLoc = `rename ($rockets+"TailLocator")`;
  1409.     parent $partTailLoc $rockets;
  1410.  
  1411.     hide  $partHeadLoc;
  1412.     hide  $partTailLoc;
  1413.     setAttr -lock true ($partHeadLoc+".rx");
  1414.     setAttr -lock true ($partHeadLoc+".ry");
  1415.     setAttr -lock true ($partHeadLoc+".rz");
  1416.     setAttr -lock true ($partHeadLoc+".sx");
  1417.     setAttr -lock true ($partHeadLoc+".sy");
  1418.     setAttr -lock true ($partHeadLoc+".sz");
  1419.     setAttr -lock true ($partTailLoc+".rx");
  1420.     setAttr -lock true ($partTailLoc+".ry");
  1421.     setAttr -lock true ($partTailLoc+".rz");
  1422.     setAttr -lock true ($partTailLoc+".sx");
  1423.     setAttr -lock true ($partTailLoc+".sy");
  1424.     setAttr -lock true ($partTailLoc+".sz");
  1425.  
  1426.     // Point-constrain each particle locator to the corresponding gravity locator.
  1427.     //
  1428.     select -r $gravHeadLoc;
  1429.     select -add $partHeadLoc;
  1430.     pointConstraint -weight 1;
  1431.  
  1432.     select -r $gravTailLoc;
  1433.     select -add $partTailLoc;
  1434.     pointConstraint -weight 1;
  1435.  
  1436.     // Add attributes for the "local gravity" to the particle shape.
  1437.     //
  1438.     addAttr -ln localGravX -at double  $rocketsShape;
  1439.     setAttr -e -keyable false ($rocketsShape+".localGravX");
  1440.     addAttr -ln localGravY -at double  $rocketsShape;
  1441.     setAttr -e -keyable false ($rocketsShape+".localGravY");
  1442.     addAttr -ln localGravZ -at double  $rocketsShape;
  1443.     setAttr -e -keyable false ($rocketsShape+".localGravZ");
  1444.  
  1445.     // Use plusMinusAverage node to compute "localGravity" as difference of the two 
  1446.     // particle space locators.  
  1447.     //
  1448.     string $pma = `createNode plusMinusAverage`;
  1449.     setAttr ($pma+".operation") 2;
  1450.     connectAttr -f ($partHeadLoc+".translate") ($pma+".input3D[0]");
  1451.     connectAttr -f ($partTailLoc+".translate") ($pma+".input3D[1]");
  1452.     connectAttr -f ($pma+".output3Dx") ($rocketsShape+".localGravX");
  1453.     connectAttr -f ($pma+".output3Dy") ($rocketsShape+".localGravY");
  1454.     connectAttr -f ($pma+".output3Dz") ($rocketsShape+".localGravZ");
  1455.  
  1456.     string $returns[2];
  1457.     $returns[0] = $gravHeadLoc;
  1458.     $returns[1] = $gravTailLoc;
  1459.  
  1460.     return $returns;
  1461. }
  1462.  
  1463.  
  1464. //  ================ createRockets ================
  1465. //
  1466. //  SYNOPSIS
  1467. //      Create the fireworks rocket particles, and their gravity field,
  1468. //        associated locators, and the position particles that will show
  1469. //        the burst and launch positions.
  1470. //        We can't create the expression here, because we have to wait
  1471. //        until the burst sparks are created, since they are referenced in
  1472. //        the expression.
  1473. //
  1474. //    ARGUMENTS
  1475. //        string $fireworksName    -- base fireworks name
  1476. //        string fireworksGroup    -- name of the top level group
  1477. //        int    $numRockets       -- number of particles/rockets to create 
  1478. //        vector $launchPosition   -- launch position
  1479. //        vector $burstAreaCenter  -- center of burst area
  1480. //        vector $burstAreaExtents -- extents of burst area
  1481. //        float  $firstLaunchFrame -- frame for first launch
  1482. //        float  $launchRate       -- launch rate (in frames)
  1483. //        float  $minFlightTimeInFrames
  1484. //        float  $maxFlightTimeInFrames
  1485. //
  1486. //    RETURN
  1487. //        The rockets particle shape, group, and gravity names
  1488. //
  1489. proc string[] createRockets(string $fireworksName,
  1490.                             string $fireworksGroup,
  1491.                             int $numRockets,
  1492.                             vector $launchPosition,
  1493.                             vector $burstAreaCenter,
  1494.                             vector $burstAreaExtents,
  1495.                             float $firstLaunchFrame,
  1496.                             float $launchRate,
  1497.                             float $minFlightTimeInFrames,
  1498.                             float $maxFlightTimeInFrames)
  1499. {
  1500.     string $result[];
  1501.     select -cl;
  1502.  
  1503.     // Create the rockets particle object.
  1504.     //
  1505.     $result = `particle -n ($fireworksName + "Rockets")`;
  1506.     string $rockets = $result[0];
  1507.     string $rocketsShape = $result[1];
  1508.  
  1509.     // Set the particle render type to be numerical, showing ids.
  1510.     //
  1511.     setAttr ($rocketsShape + ".particleRenderType") 3;
  1512.  
  1513.     // Enable the rocket shape's display handle and position it at 1 above the
  1514.     // centroid of the rocket particle.
  1515.     //
  1516.     setAttr ($rockets + ".displayHandle") 1;
  1517.  
  1518.     expression -n ($fireworksName + "RocketSelectHandleExpr") -ae 0
  1519.         -s 
  1520.         ( "selectHandleX = " + $rocketsShape + ".centroidX;\n"
  1521.         + "selectHandleY = " + $rocketsShape + ".centroidY + 1.5;\n"
  1522.         + "selectHandleZ = " + $rocketsShape + ".centroidZ ;")
  1523.         -o $rockets;
  1524.  
  1525.     // Create and connect the gravity field for the rockets.
  1526.     //
  1527.     select -cl;
  1528.     clear($result);
  1529.     string $rocketsGravity;
  1530.     $result = `gravity -n ($rockets + "Gravity")`;
  1531.     $rocketsGravity = $result[0];
  1532.     connectDynamic -f $rocketsGravity $rocketsShape;
  1533.  
  1534.     addAndConnectRocketAttributes($fireworksGroup,
  1535.                                     $rocketsShape, 
  1536.                                     $rocketsGravity );
  1537.  
  1538.     string $gravityLocators[];
  1539.     $gravityLocators =
  1540.         makeGravityLocatorsForRockets( $rockets, $rocketsShape, $rocketsGravity );
  1541.  
  1542.     string $gravityHeadLoc = $gravityLocators[0];
  1543.     string $gravityTailLoc = $gravityLocators[1];
  1544.  
  1545.     createRocketParticles( $rocketsShape, 
  1546.                             $numRockets, 
  1547.                             $launchPosition, 
  1548.                             $burstAreaCenter,
  1549.                             $burstAreaExtents, 
  1550.                             $firstLaunchFrame, 
  1551.                             $launchRate, 
  1552.                             $minFlightTimeInFrames,
  1553.                             $maxFlightTimeInFrames );
  1554.  
  1555.  
  1556.     // hide the attributes the user won't use, to make the UI simpler
  1557.     //
  1558.     hideAndLockRocketAttributes( $rocketsShape );
  1559.  
  1560.     // Create two "shadow" particle objects, each with "numRockets" number
  1561.     // of particles.  One will be turned on/off to show/hide the launch
  1562.     // positions of the "real" rockets, and the other will be turned on/off
  1563.     // to show/hide the burst positions of the "real" rockets.
  1564.     //
  1565.     string $launchParticleShape =
  1566.         createRocketPositionParticles($fireworksName, $numRockets, $rocketsShape);
  1567.     
  1568.     // Note that there is still the rockets expression to write.  It controls
  1569.     // the flight of the rockets and issues the emit action for the burst.
  1570.     // It can be created only after the burst sparks particle has been
  1571.     // created.
  1572.  
  1573.     // Put all the rocket objects in one group and return the rocket shape
  1574.     // and the rocket group names.
  1575.     //
  1576.     string $rocketsGroup = 
  1577.             `group -n ($rockets + "Group") 
  1578.                     $rockets 
  1579.                     $rocketsGravity
  1580.                     $gravityHeadLoc
  1581.                     $gravityTailLoc`;
  1582.  
  1583.     hide $rocketsGravity;
  1584.  
  1585.     string $returns[4];
  1586.     $returns[0] = $rocketsShape;
  1587.     $returns[1] = $rocketsGroup;
  1588.     $returns[2] = $rocketsGravity;
  1589.     $returns[3] = $launchParticleShape;
  1590.  
  1591.     return $returns;
  1592. }
  1593.  
  1594.  
  1595. // ************************************************************************ //
  1596. //                                                                          //
  1597. //                     ROCKETS TRAIL SPARKS PROCEDURES                      //
  1598. //                                                                          //
  1599. // ************************************************************************ //
  1600.  
  1601. //
  1602. //
  1603. //  ================ createRocketTrailsExpression ================
  1604. //
  1605. //  SYNOPSIS
  1606. //      Create the creation expression to set the lifespan of the rocket trail sparks.
  1607. //        Create the runtime expression to randomly pick the color of each spark,
  1608. //        within the color spectrum set up for them.
  1609. //
  1610. //    ARGUMENTS
  1611. //        string $fireworksGroup --
  1612. //        string $rocketsShape   --
  1613. //        string $rocketTrailShape -- the rocket sparks particle shape
  1614. //        string $trailInstancer -- the rocket trail particle instancer (w/cones)
  1615. //
  1616. //    RETURN
  1617. //        None
  1618. //
  1619. proc createRocketTrailsExpression(string $fireworksGroup,
  1620.                                     string $rocketsShape,
  1621.                                     string $rocketTrailShape, 
  1622.                                     string $trailInstancer)
  1623. {
  1624.     // Set the lifespan to between 1/2 and 1 1/2 seconds.
  1625.     // And set the color for each particle emitted.
  1626.     //
  1627.     dynExpression -c 
  1628.         -s 
  1629.         ( "lifespanPP = rand(.5,1.5);\n\n"
  1630.         + "// Get the number of cones of diffent colors to be instanced to\n"
  1631.         + "// the particles in the trail, and choose one randomly.\n"
  1632.         + "//\n"
  1633.         + "float $indexCount = " + $trailInstancer + ".hierarchyCount;\n"
  1634.         + "int $whichIndex = rand($indexCount);\n"
  1635.         + "\n"
  1636.         + "sparkColorIndex = $whichIndex;\n"
  1637.         + "float $sparksColor[] = `getAttr " + $rocketsShape + ".rocketTrailColors[$whichIndex]`;\n"
  1638.         + "rgbPP = <<$sparksColor[0], $sparksColor[1], $sparksColor[2]>>;\n")
  1639.         $rocketTrailShape;
  1640.  
  1641.     dynExpression -r
  1642.         -s ("float $s =  max(minTailSize, min(maxTailSize, mag(velocity) * 5));\n"
  1643.         + "float $ratio = age/lifespanPP;\n"
  1644.         + "$s = (1.0-smoothstep(.5,1,$ratio)) * $s;\n"
  1645.         + "trailSparkScale = <<$s,1,1>>;")
  1646.         $rocketTrailShape;
  1647.  
  1648.  
  1649. }
  1650.  
  1651.  
  1652. //
  1653. //  ================ createRocketTrailsConeInstances ================
  1654. //
  1655. //  SYNOPSIS
  1656. //      Create a particleInstancer, and a set of instances (cones) for it and
  1657. //        instance it to the particle rocket sparks to get the software rendered colors
  1658. //        and glow intensity needed. 
  1659. //
  1660. //    ARGUMENTS
  1661. //        string $fireworksGroup     -- name of the top level fireworks group
  1662. //        string $trailParticle      -- name of particle to instance cones to
  1663. //        string $trailparticleShape -- name of particle shape to instance cones to
  1664. //        string $rocketsShape       -- name of rockets particle shape
  1665. //        string $colorCreationProc  -- name of procedure to call to create colors
  1666. //        int    $numInstances       -- number of instances/colors to create
  1667. //
  1668. //    RETURN
  1669. //        The name of the particleInstancer and the group consisting of the cones.
  1670. //
  1671. proc string[] createRocketTrailsConeInstances(string $fireworksGroup,
  1672.                                                     string $trailParticle,
  1673.                                                     string $trailParticleShape,
  1674.                                                     string $rocketsShape,
  1675.                                                     string $colorCreationProc,
  1676.                                                     int $numInstances)
  1677. {
  1678.     vector $colors[];
  1679.     string $theCones[];
  1680.  
  1681.     clear($colors);
  1682.     clear($theCones);
  1683.  
  1684.     // Set the colors for the cone instances.  The colors are in "rgb".
  1685.     //
  1686.     if (size($colorCreationProc) == 0)
  1687.     {
  1688.         $colors = defaultRocketTrailColors($numInstances);
  1689.     }
  1690.     else
  1691.     {
  1692.         string $colorCmd = $colorCreationProc + " " + $numInstances;
  1693.         $colors = eval($colorCmd);
  1694.     }
  1695.  
  1696.     // Set the cone axis and scale.
  1697.     //
  1698.     vector $coneAxis = <<-1,0,0>>;
  1699.     vector $coneScale = <<.3,.05,.05>>;
  1700.  
  1701.     // Create the cones and shaders, using the colors just created.
  1702.     //
  1703.     // createColoredStreaks returns the name of the cone and the name of
  1704.     // it shader.  Here we want to use only the cone name.
  1705.     //
  1706.     string $returns[];
  1707.     for ($i = 0; $i < $numInstances; $i++)
  1708.     {
  1709.         // First arg of is the name of the cone object to be created, second arg
  1710.         // is the name of the shader to be created.
  1711.         //
  1712.         $returns = createColoredStreaks( "trail",
  1713.                                             ($trailParticle +"ColorCone" + $i), 
  1714.                                             ($trailParticle +"Color" + $i), 
  1715.                                             $coneScale,
  1716.                                             $coneAxis,
  1717.                                             $colors[$i]);
  1718.         $theCones[$i] = $returns[0];
  1719.         $theShader = $returns[1];
  1720.  
  1721.         // Set the colors in the color palette, for easy access.
  1722.         //
  1723.         vector $rgb = $colors[$i];
  1724.         float $r = $rgb.x;
  1725.         float $g = $rgb.y;
  1726.         float $b = $rgb.z;
  1727.  
  1728.         setAttr ($rocketsShape + ".rocketTrailColors[" + $i + "]") 
  1729.             -type double3 $r $g $b;
  1730.  
  1731.         // Connect the shader color for this fireworks color to the 
  1732.         // fireworksShaders multi, so we can get to it for editing.
  1733.         //
  1734.         connectAttr
  1735.             ($theShader + ".message")
  1736.             ($rocketsShape + ".rocketTrailShaders[" + $i + "]");
  1737.     }
  1738.  
  1739.     // Create the particle instancer and add the color cones to it.
  1740.     //
  1741.     //    "-objectIndex sparkColorIndex" -- set the index of the object to be instanced
  1742.     //    to be the same as the particle's "sparkColorIndex", which will be set randomly
  1743.     //    in the rocket particle's expression.
  1744.     // 
  1745.     //    "-aimDirection velocity"  Make the cones aim in the direction of the
  1746.     //    particle's velocity.
  1747.     //
  1748.     //    "-addObject" sets of the string of objects to be added to the instancer,
  1749.     //    which is all the cones.
  1750.     //
  1751.     //    "-object ..."  the objects to be added.
  1752.     //
  1753.     string $instancerName = $trailParticle + "Instancer";
  1754.     string $cmd = "particleInstancer -name " + $instancerName + " ";
  1755.     $cmd += "-position position -objectIndex sparkColorIndex ";
  1756.     $cmd += "-aimDirection velocity -scale trailSparkScale -addObject ";
  1757.  
  1758.     for ($i = 0; $i < size($theCones); $i++)
  1759.     {
  1760.         $cmd += " -object " + $theCones[$i] + " " ;
  1761.     }
  1762.  
  1763.     $cmd += $trailParticleShape;
  1764.  
  1765.     // Issue the particleInstancer command.
  1766.     //
  1767.     string $result = eval($cmd);
  1768.  
  1769.     // Now group all the cones, so they won't overwhelm the outliner and can be
  1770.     // easily identified and hidden.
  1771.     //
  1772.     select -cl;
  1773.     for ($i = 0; $i < size($theCones); $i++)
  1774.     {
  1775.         select -add $theCones[$i];
  1776.     }
  1777.     string $group = `group -n ($trailParticle + "ColorCones")`;
  1778.     hide $group;
  1779.  
  1780.     // Return the name of the particleInstancer and cones group
  1781.     //
  1782.     string $returns[2];
  1783.     $returns[0] = $result;
  1784.     $returns[1] = $group;
  1785.     return $returns;
  1786. }
  1787.  
  1788.  
  1789. //
  1790. //  ================ createRocketTrails ================
  1791. //
  1792. //  SYNOPSIS
  1793. //      Create the particle object holding the rocket trail sparks
  1794. //
  1795. //    ARGUMENTS
  1796. //        string $fireworksGroup    -- name of the top level fireworks group
  1797. //        string $fireworksName     -- base name of the fireworks
  1798. //        string $rocketsShape      -- rockets particle shape
  1799. //        string $colorCreationProc -- proc to call to create colors
  1800. //        int    $numColors         -- number of cones/shaders to create
  1801. //
  1802. //    RETURN
  1803. //        The name of the trail sparks particle shape and the name of the
  1804. //        trail group (the group consists of the rocket trails particle,
  1805. //        the particle instancer, the cones and the gravity field).
  1806. //
  1807. proc string[] createRocketTrails(string $fireworksGroup,
  1808.                                         string $fireworksName, 
  1809.                                         string $rocketsShape,
  1810.                                         string $colorCreationProc,
  1811.                                         int $numColors)
  1812. {
  1813.     // Create the particle that will hold the sparks of the rocket trails.
  1814.     //
  1815.     string $result[];
  1816.     $result = `particle -n ($fireworksName + "RocketTrails")`;
  1817.     string $rocketSparks = $result[0];
  1818.     string $rocketTrailShape = $result[1];
  1819.  
  1820.     // Set lifespan mode to use lifespanPP.
  1821.     //
  1822.     setAttr ($rocketTrailShape+".lifespanMode") 3;
  1823.  
  1824.     // Add the attributes sparkColorIndex, which will hold the index
  1825.     // of the object in the instancer to instance to each particle
  1826.     //
  1827.     addAttr -ln "sparkColorIndex" -dt doubleArray $rocketTrailShape;
  1828.     addAttr -ln "sparkColorIndex0" -dt doubleArray $rocketTrailShape;
  1829.  
  1830.     addAttr -ln "rgbPP" -dt vectorArray $rocketTrailShape;
  1831.     addAttr -ln "rgbPP0" -dt vectorArray $rocketTrailShape;
  1832.  
  1833.     addAttr -ln "trailSparkScale" -dt vectorArray $rocketTrailShape;
  1834.     addAttr -ln "trailSparkScale0" -dt vectorArray $rocketTrailShape;
  1835.  
  1836.     // Set the render type to be points.
  1837.     //
  1838.     setAttr ($rocketTrailShape + ".particleRenderType") 6;
  1839.  
  1840.     addAttr -is true -ln "lineWidth" -at long -min 1 -max 20 -dv 2
  1841.         $rocketTrailShape;
  1842.     addAttr -is true -ln "tailSize" -at "float" -min 0 -max 100 -dv 10 
  1843.         $rocketTrailShape;
  1844.  
  1845.     addAttr -is true -ln "minTailSize" 
  1846.             -at "float" -min 0 -dv .5 
  1847.             $rocketTrailShape;
  1848.     addAttr -is true -ln "maxTailSize" 
  1849.             -at "float" -min 0 -dv 2 
  1850.             $rocketTrailShape;
  1851.  
  1852.     // Create the instancer, its cones, and their shaders.
  1853.     // The proc returns the name of the particle instancer and the cones group.
  1854.     //
  1855.     string $returns[] =
  1856.             createRocketTrailsConeInstances($fireworksGroup,
  1857.                                                 $rocketSparks, 
  1858.                                                 $rocketTrailShape,
  1859.                                                 $rocketsShape,
  1860.                                                 $colorCreationProc,
  1861.                                                 $numColors);
  1862.     string $trailsInstancer = $returns[0];
  1863.     string $trailsConesGroup = $returns[1];
  1864.  
  1865.     // Create the expressions that will set the lifespan and the sparkColorIndex
  1866.     // to set which cone will be instanced to each trail particle.
  1867.     //
  1868.     createRocketTrailsExpression($fireworksGroup,
  1869.                                     $rocketsShape,
  1870.                                     $rocketTrailShape, 
  1871.                                     $trailsInstancer);
  1872.  
  1873.     // If start time of animation is negative, make the particle shape start there.
  1874.     // 
  1875.     float $animStart = `playbackOptions -q -min`;
  1876.     if ($animStart < `getAttr ($rocketTrailShape + ".startFrame")`)
  1877.     {
  1878.         setAttr ($rocketTrailShape + ".startFrame") $animStart;
  1879.     }
  1880.  
  1881.     // Create the gravity and drag fields for the rocket sparks.
  1882.     //
  1883.     select -cl;
  1884.     clear($result);
  1885.     $result = `gravity -m 2.5 -n ($fireworksName + "RocketTrailsGravity")`;
  1886.     string $rocketTrailsGravity = $result[0];
  1887.  
  1888.     select -cl;
  1889.     clear($result);
  1890.     $result = `drag -m 2 -n ($fireworksName + "RocketTrailsDrag")`;
  1891.     $rocketTrailsDrag = $result[0];
  1892.  
  1893.     connectDynamic -f $rocketTrailsGravity -f $rocketTrailsDrag $rocketTrailShape;
  1894.  
  1895.     select -r $rocketTrailShape $trailsInstancer $trailsConesGroup $rocketTrailsGravity $rocketTrailsDrag;
  1896.  
  1897.     // Group all the objects created for trails.
  1898.     //
  1899.     string $trailsGroup = `group -n ($fireworksName + "TrailSparksGroup")`;
  1900.  
  1901.     hide $rocketTrailsDrag $rocketTrailsGravity;
  1902.  
  1903.     string $returns[3];
  1904.     $returns[0] = $rocketTrailShape;
  1905.     $returns[1] = $trailsGroup;
  1906.     $returns[2] = $trailsInstancer;
  1907.     return $returns;
  1908. }
  1909.  
  1910.  
  1911. // ************************************************************************ //
  1912. //                                                                          //
  1913. //                        BURST SPARKS PROCEDURES                           //
  1914. //                                                                          //
  1915. // ************************************************************************ //
  1916.  
  1917.  
  1918. //
  1919. //  ================ createBurstSparksExpression ================
  1920. //
  1921. //  SYNOPSIS
  1922. //      Create the creation expression to set the lifespan and sparkScale of the
  1923. //        burst sparks.
  1924. //        Create the runtime expression to control the scale of the particles.
  1925. //
  1926. //        The sparkScale (how long the cones are) is dependent on the velocity of
  1927. //        the sparks.
  1928. //
  1929. //    ARGUMENTS
  1930. //        string $sparksShape
  1931. //        string $particleInstancer
  1932. //
  1933. //    RETURN
  1934. //        None
  1935. //
  1936. proc createBurstSparksExpression(string $fireworksGroup,
  1937.                                     string $sparksShape, 
  1938.                                     string $particleInstancer)
  1939. {
  1940.     // Set both the creation and runtime expressions to set the scale factor for
  1941.     // the length of each particle in a particle burst. It is based on the
  1942.     // velocity of the particle (faster is // longer) and age (older is shorter),
  1943.     // but within the range of minTailSize and maxTailSize.
  1944.     //
  1945.     dynExpression -c 
  1946.         -s ("lifespanPP = rand(3,4);\n"
  1947.          + "float $s = 0.0;"
  1948.          + "sparkScale = <<$s,1,1>>;\n")
  1949.         $sparksShape;
  1950.  
  1951.     dynExpression -r
  1952.         -s ("float $s =  max(minTailSize, min(maxTailSize, mag(velocity) * .05));\n"
  1953.         + "float $ratio = age/lifespanPP;\n"
  1954.         + "$s = (1.0-smoothstep(.9,1,$ratio)) * $s;\n"
  1955.         + "sparkScale = <<$s,1,1>>;")
  1956.         $sparksShape;
  1957. }
  1958.  
  1959.  
  1960. //
  1961. //  ================ createBurstSparksConeInstances ================
  1962. //
  1963. //  SYNOPSIS
  1964. //      Create a particleInstancer, and a set of instances (cones) for it and
  1965. //        instance it to the particle sparks to get the software rendered colors
  1966. //        and glow intensity needed. 
  1967. //
  1968. //    ARGUMENTS
  1969. //        string $fireworksGroup           -- name of the top level fireworks group
  1970. //        string $burstSparksParticle      -- name of particle to instance cones to
  1971. //        string $burstSparksParticleShape -- particle shape to instance cones to
  1972. //        string $rocketsShape             -- rockets particle shape
  1973. //        string $colorCreationProc        -- proc to create colors for sparks
  1974. //        int $numInstances                 -- number of colors/cones to create.
  1975. //
  1976. //    RETURN
  1977. //        The name of the particleInstancer and the burst sparks cones group.
  1978. //
  1979. proc string[] createBurstSparksConeInstances(string $fireworksGroup,
  1980.                                                 string $burstSparksParticle,
  1981.                                                 string $burstSparksParticleShape,
  1982.                                                 string $rocketsShape,
  1983.                                                 string $colorCreationProc,
  1984.                                                 int $numInstances)
  1985. {
  1986.     vector $colors[];
  1987.     string $theCones[];
  1988.  
  1989.     // Set the colors for the cone instances.  This proc will set $numInstances
  1990.     // of colors, ranging equally along the color spectrum in color spectrum
  1991.     // order.  The colors are "rgb".
  1992.     //
  1993.     if (size($colorCreationProc) == 0)
  1994.     {
  1995.         $colors = defaultFireworksColors($numInstances);
  1996.         setAttr -type "string"
  1997.             ($rocketsShape + ".colorPaletteProc") "defaultFireworksColors";
  1998.     }
  1999.     else
  2000.     {
  2001.         string $colorCmd = $colorCreationProc + " " + $numInstances;
  2002.         $colors = eval($colorCmd);
  2003.         setAttr -type "string"
  2004.                 ($rocketsShape + ".colorPaletteProc") $colorCreationProc;
  2005.     }
  2006.  
  2007.     // Set the cone axis and scale.
  2008.     //
  2009.     vector $coneAxis = <<-1, 0, 0>>;
  2010.     vector $coneScale = <<1,.1,.1>>;
  2011.  
  2012.     // Create the cones and shaders, using the colors just created.
  2013.     //
  2014.     string $returns[];
  2015.     string $theShader;
  2016.     for ($i = 0; $i < $numInstances; $i++)
  2017.     {
  2018.         // First arg of is the name of the cone object to be created, second arg
  2019.         // is the name of the shader to be created.
  2020.         //
  2021.         $returns = 
  2022.                 createColoredStreaks("sparks", 
  2023.                                         ($burstSparksParticle + "ColorCone" + $i), 
  2024.                                         ($burstSparksParticle + "Color" + $i), 
  2025.                                         $coneScale,
  2026.                                         $coneAxis,
  2027.                                         $colors[$i]);
  2028.         $theCones[$i] = $returns[0];
  2029.         $theShader = $returns[1];
  2030.  
  2031.         // Set the colors in the color palette, for easy access.
  2032.         //
  2033.         vector $rgb = $colors[$i];
  2034.         float $r = $rgb.x;
  2035.         float $g = $rgb.y;
  2036.         float $b = $rgb.z;
  2037.  
  2038.         setAttr ($rocketsShape + ".fireworksColors[" + $i + "]") 
  2039.             -type double3 $r $g $b;
  2040.  
  2041.         // Connect the shader color for this fireworks color to the 
  2042.         // fireworksShaders multi, so we can get to it for editing.
  2043.         //
  2044.         connectAttr
  2045.             ($theShader + ".message")
  2046.             ($rocketsShape + ".fireworksShaders[" + $i + "]");
  2047.     }
  2048.  
  2049.     // Create the particle instancer and add the color cones to it.
  2050.     //
  2051.     //    "-objectIndex sparkColorIndex" -- set the index of the object to be
  2052.     //    instanced to be the same as the particle's "sparkColorIndex", which
  2053.     //    will be set randomly in the particle burst's expression.
  2054.     // 
  2055.     //    "-aimDirection velocity"  Make the cones aim in the direction of
  2056.     //    the particle's velocity.
  2057.     //
  2058.     //    "-scale sparkScale"  Set the scaling of the cone to the the
  2059.     //    particle's sparkScale, which will be based on the velocity of the
  2060.     //    particle in the particle's expression.
  2061.     //
  2062.     //    "-addObject" sets of the string of objects to be added to the
  2063.     //    instancer, which is all the cones.
  2064.     //
  2065.     //    "-object ..."  the objects to be added.
  2066.     //
  2067.     string $instancerName = $burstSparksParticle + "Instancer";
  2068.     string $cmd = "particleInstancer -name " + $instancerName  + " ";
  2069.     $cmd += "-position position -objectIndex sparkColorIndex ";
  2070.     $cmd += "-aimDirection velocity -scale sparkScale -addObject ";
  2071.  
  2072.     for ($i = 0; $i < size($theCones); $i++)
  2073.     {
  2074.         $cmd += " -object " + $theCones[$i] + " " ;
  2075.     }
  2076.  
  2077.     $cmd += $burstSparksParticleShape;
  2078.  
  2079.     // Issue the particleInstancer command.
  2080.     //
  2081.     string $result = eval($cmd);
  2082.  
  2083.     // Now group all the cones, so they won't overwhelm the outliner and can be
  2084.     // easily hidden.
  2085.     //
  2086.     select -cl;
  2087.     for ($i = 0; $i < size($theCones); $i++)
  2088.     {
  2089.         select -add $theCones[$i];
  2090.     }
  2091.     string $group = `group -n ($burstSparksParticle + "ColorCones")`;
  2092.     hide $group;
  2093.  
  2094.     // Return the name of the particleInstancer and cone group.
  2095.     //
  2096.     string $returns[2];
  2097.     $returns[0] = $result;
  2098.     $returns[1] = $group;
  2099.     return $returns;
  2100. }
  2101.  
  2102.  
  2103. //
  2104. //  ================ createBurstSparks ================
  2105. //
  2106. //  SYNOPSIS
  2107. //      Create the particle object holding the fireworks burst sparks.
  2108. //
  2109. //    ARGUMENTS
  2110. //        string $fireworksGroup    -- the top level fireworks group name
  2111. //        string $fireworksName     -- the fireworks base name
  2112. //        string $rocketsShape      -- rockets particle shape
  2113. //        string $colorCreationProc -- proc to create sparks colors
  2114. //        int    $numSparksColors   -- number of colors/cone instances to create
  2115. //
  2116. //    RETURN
  2117. //        The names of the sparks particle shape, the particle instancer, and the
  2118. //        sparks group, which consists of the sparks particle, the gravity and
  2119. //        drag fields, the particle instancer, and the cones group.
  2120. //
  2121. proc string[] createBurstSparks(string $fireworksGroup,
  2122.                                 string $fireworksName, 
  2123.                                 string $rocketsShape,
  2124.                                 string $colorCreationProc,
  2125.                                 int $numSparksColors)
  2126. {
  2127.     // The sparks are created by an "emit" action in the runtime expression for
  2128.     // rockets particle.  This procedure creates the particle object into which
  2129.     // the emit action emits the sparks, sets its attributes, and creates its
  2130.     // controlling expressions and the instancer with the color cones that give
  2131.     // the sparks their color and glow.
  2132.     //
  2133.  
  2134.     // Create the sparks particle
  2135.     //
  2136.     string $result[];
  2137.     $result = `particle -n ($fireworksName + "BurstSparks")`;
  2138.     string $sparks = $result[0];
  2139.     string $sparksShape = $result[1];
  2140.  
  2141.     // Set the render type.
  2142.     //
  2143.     setAttr ($sparksShape + ".particleRenderType") 6;
  2144.     addAttr -is true -ln "lineWidth" -at long -min 1 -max 20 -dv 2
  2145.         $sparksShape;
  2146.     addAttr -is true -ln "tailSize" -at "float" -min 0 -max 100 -dv 5 
  2147.         $sparksShape;
  2148.  
  2149.     // Add attributes for the min and max tail size (length of the cones).
  2150.     // These are used in the scaling of the cones based on their veloctiy.
  2151.     //
  2152.     addAttr -is true -ln "minTailSize" -at "float" -min 0 -dv 1 $sparksShape;
  2153.     addAttr -is true -ln "maxTailSize" -at "float" -min 0 -dv 1 $sparksShape;
  2154.  
  2155.     setAttr -keyable on ($sparksShape + ".minTailSize");
  2156.     setAttr -keyable on ($sparksShape + ".maxTailSize");
  2157.  
  2158.     setAttr ($sparksShape + ".minTailSize") .5;
  2159.     setAttr ($sparksShape + ".maxTailSize") 2;
  2160.  
  2161.     // set lifespan mode to use lifespanPP
  2162.     //
  2163.     setAttr ($sparksShape+".lifespanMode") 3;
  2164.  
  2165.     // Add per particle attributes
  2166.     // Used to scale the length of the color cones based on their velocity
  2167.     // and age.
  2168.     //
  2169.     addAttr -ln "sparkScale" -dt vectorArray $sparksShape;
  2170.     addAttr -ln "sparkScale0" -dt vectorArray $sparksShape;
  2171.  
  2172.     // Used to index into the particle instancer to choose one of the color
  2173.     // cones.
  2174.     //
  2175.     addAttr -ln "sparkColorIndex" -dt doubleArray $sparksShape;
  2176.     addAttr -ln "sparkColorIndex0" -dt doubleArray $sparksShape;
  2177.  
  2178.     // Used to set the particle colors to be the same as the instancer
  2179.     // colors for each spark.
  2180.     //
  2181.     addAttr -ln "rgbPP" -dt vectorArray $sparksShape;    
  2182.     addAttr -ln "rgbPP0" -dt vectorArray $sparksShape;    
  2183.  
  2184.     // If start time of animation is negative, make the particle shape start there.
  2185.     // 
  2186.     float $animStart = `playbackOptions -q -min`;
  2187.     if ($animStart < `getAttr ($sparksShape + ".startFrame")`)
  2188.     {
  2189.         setAttr ($sparksShape + ".startFrame") $animStart;
  2190.     }
  2191.  
  2192.     // Create and connect a gravity field for the sparks.
  2193.     //
  2194.     clear($result);
  2195.     select -cl;
  2196.     string $rocketsGravity;
  2197.     $result = `gravity -n ($fireworksName + "SparksGravity")`;
  2198.     $sparksGravity = $result[0];
  2199.  
  2200.     // Create a drag field for the burst sparks.
  2201.     //
  2202.     select -cl;
  2203.     clear($result);
  2204.     $result = `drag -m 2 -n ($fireworksName + "SparksDrag")`; 
  2205.     string $sparksDrag = $result[0];
  2206.  
  2207.     // Connect the sparks to the drag and gravity fields.
  2208.     //
  2209.     connectDynamic -f $sparksDrag -f $sparksGravity $sparksShape;
  2210.  
  2211.     // Create the render cones, shaders and instancer for the sparks;
  2212.     // The sparks will be instanced to cones, for each of which a shader
  2213.     // will be produced.  The shaders will have a range of colors, and
  2214.     // an expression in the "rocket" particle object will cycle through
  2215.     // the cones/shaders for each particle in sparksShape.
  2216.     //
  2217.     string $returns[] =
  2218.             createBurstSparksConeInstances ($fireworksGroup,
  2219.                                             $sparks, 
  2220.                                             $sparksShape, 
  2221.                                             $rocketsShape,
  2222.                                             $colorCreationProc,
  2223.                                             $numSparksColors); 
  2224.  
  2225.     string $sparksInstancer =  $returns[0];
  2226.     string $conesGroup = $returns[1];
  2227.  
  2228.     // Create the expression for the Sparks.
  2229.     //
  2230.     createBurstSparksExpression($fireworksGroup, 
  2231.                                 $sparksShape, 
  2232.                                 $sparksInstancer);
  2233.  
  2234.     // Group all the objects relating to the sparks.
  2235.     //
  2236.     select -r $sparks $sparksGravity $sparksDrag $sparksInstancer $conesGroup;
  2237.  
  2238.     $sparksGroup = `group -n ($fireworksName + "BurstSparksGroup")`;
  2239.  
  2240.     hide $sparksDrag $sparksGravity;
  2241.  
  2242.     clear($result);
  2243.     $result[0] = $sparksShape;
  2244.     $result[1] = $sparksInstancer;
  2245.     $result[2] = $sparksGroup;
  2246.     return $result;
  2247. }
  2248.  
  2249. //  ================ createRocketPositionLocators ================
  2250. //
  2251. //  SYNOPSIS
  2252. //      Create the two locators for editing start and burst positions of
  2253. //        the rocket particles.
  2254. //
  2255. //    ARGUMENTS
  2256. //        string $fireworksName   -- the fireworks base name
  2257. //        string $rocketsShape    -- the rocket particle shape
  2258. //
  2259. //    RETURN
  2260. //        Names of the two locators
  2261. //
  2262. proc string [] createRocketPositionLocators(string $fireworksName,
  2263.                                             string $rocketsShape)
  2264. {
  2265.     createPrimitive nullObject;
  2266.     string $launchPosManip = `rename ($fireworksName + "LaunchPositionManip")`;
  2267.  
  2268.     createPrimitive nullObject;
  2269.     string $burstPosManip = `rename ($fireworksName + "BurstPositionManip")`;
  2270.  
  2271.     // Make a message connection between the rockets shape and the
  2272.     // locators, so we can find the locators again in the Attribute
  2273.     // Editor.
  2274.     //
  2275.     connectAttr ($launchPosManip + ".message") 
  2276.                 ($rocketsShape + ".launchPositionManipMessage");
  2277.  
  2278.     connectAttr ($burstPosManip + ".message") 
  2279.                 ($rocketsShape + ".burstPositionManipMessage");
  2280.  
  2281.     float $launchPos1[] = 
  2282.         `particle -attribute launchPositionPP -id 0 -q $rocketsShape`;
  2283.  
  2284.     float $burstPos1[] = 
  2285.         `particle -attribute burstPositionPP -id 0 -q $rocketsShape`;
  2286.  
  2287.     setAttr ($launchPosManip + ".translate") 
  2288.             -type double3 ($launchPos1[0]) ($launchPos1[1]) ($launchPos1[2]);
  2289.     setAttr ($burstPosManip + ".translate") 
  2290.             -type double3 ($burstPos1[0]) ($burstPos1[1]) ($burstPos1[2]);
  2291.  
  2292.     string $returns[2];
  2293.     $returns[0] = $launchPosManip;
  2294.     $returns[1] = $burstPosManip;
  2295.  
  2296.     return $returns;
  2297. }
  2298.  
  2299.  
  2300. // ************************************************************************ //
  2301. //                                                                          //
  2302. //                       PROCEDURES TO SET ATTRIBUTES                       //
  2303. //                                                                          //
  2304. // ************************************************************************ //
  2305. //
  2306. //
  2307. //  ================ fwSetRocketAttributes ================
  2308. //
  2309. //  SYNOPSIS
  2310. //      Set the rocket attributes.  (Called from performDynamicsClipEffects)
  2311. //
  2312. //    ARGUMENTS
  2313. //        string $fireworks         -- the fireworks top level group name
  2314. //        int    $maxBurstSpeed     -- max burst speed of the fireworks sparks
  2315. //        int    $sparksColorSpread -- num colors per spark burst
  2316. //
  2317. //    RETURN
  2318. //        None
  2319. //
  2320. global proc fwSetRocketAttributes(string $fireworks,
  2321.                                     float $maxBurstSpeed,
  2322.                                     int $sparksColorSpread)
  2323. {
  2324.     setAttr ($fireworks + ".maxBurstSpeed") $maxBurstSpeed;
  2325.     setAttr ($fireworks + ".sparksColorSpread") $sparksColorSpread;
  2326. }
  2327.  
  2328.  
  2329. //
  2330. //  ================ fwSetTrailAttributes ================
  2331. //
  2332. //  SYNOPSIS
  2333. //      Set the rocket trail attributes.  (Called from performDynamicsClipEffects)
  2334. //
  2335. //    ARGUMENTS
  2336. //        string $fireworks     -- the fireworks top level group name
  2337. //        float  $emitRate      -- rate of the trail emitter
  2338. //        float  $emitSpeed     -- speed of the trail emitter
  2339. //      float  $emitSpread    -- spread of the trail emitter
  2340. //      float  $minTailSize   -- min size (length) of the trail particles/cones
  2341. //      float  $maxTailSize   -- max size (length) of the trail particles/cones
  2342. //      float  $glow          -- glowIntensity of the trail shaders
  2343. //      float  $incandescence -- incandescence intensity of the trail shaders
  2344. //
  2345. //    RETURN
  2346. //        None
  2347. //
  2348. global proc fwSetTrailAttributes(string $fireworks,
  2349.                                     float $emitRate,
  2350.                                     float $emitSpeed,
  2351.                                     float $emitSpread,
  2352.                                     float $minTailSize,
  2353.                                     float $maxTailSize,
  2354.                                     float $glow,
  2355.                                     float $incandescence)
  2356. {
  2357.  
  2358.     setAttr ($fireworks + ".trailEmitRate") $emitRate;
  2359.     setAttr ($fireworks + ".trailEmitSpeed") $emitSpeed;
  2360.     setAttr ($fireworks + ".trailEmitSpread") $emitSpread;
  2361.     setAttr ($fireworks + ".trailMinTailSize") $minTailSize;
  2362.     setAttr ($fireworks + ".trailMaxTailSize") $maxTailSize;
  2363.     setAttr ($fireworks + ".trailGlow") $glow;
  2364.     setAttr ($fireworks + ".trailIncandescence") $incandescence;
  2365.  
  2366. }
  2367.  
  2368.  
  2369. //
  2370. //  ================ fwSetSparksAttributes ================
  2371. //
  2372. //  SYNOPSIS
  2373. //      Set the burst sparks attributes.  (Called from performDynamicsClipEffects)
  2374. //
  2375. //    ARGUMENTS
  2376. //        string $fireworks      -- the fireworks top level group name
  2377. //        float  $minSparksCount -- min number of sparks per burst
  2378. //        float  $maxSparksCount -- max number of sparks per burst
  2379. //      float  $minTailSize    -- min size (length) of the sparks particles/cones
  2380. //      float  $maxTailSize    -- max size (length) of the sparks particles/cones
  2381. //      float  $glow           -- glowIntensity of the sparks shaders
  2382. //      float  $incandescence  -- incandescence intensity of the sparks shaders
  2383. //
  2384. //    RETURN
  2385. //        None
  2386. //
  2387. global proc fwSetSparksAttributes(string $fireworks,
  2388.                                     int $minSparksCount,
  2389.                                     int $maxSparksCount,
  2390.                                     float $minTailSize,
  2391.                                     float $maxTailSize,
  2392.                                     float $glow,
  2393.                                     float $incandescence)
  2394. {
  2395.  
  2396.     setAttr ($fireworks + ".minSparksCount") $minSparksCount;
  2397.     setAttr ($fireworks + ".maxSparksCount") $maxSparksCount;
  2398.     setAttr ($fireworks + ".sparksMinTailSize") $minTailSize;
  2399.     setAttr ($fireworks + ".sparksMaxTailSize") $maxTailSize;
  2400.     setAttr ($fireworks + ".sparksGlow") $glow;
  2401.     setAttr ($fireworks + ".sparksIncandescence") $incandescence;
  2402.  
  2403. }
  2404.  
  2405.  
  2406. // ************************************************************************ //
  2407. //                                                                          //
  2408. //                           MAIN PROCEDURE                                 //
  2409. //                                                                          //
  2410. // ************************************************************************ //
  2411.  
  2412. //
  2413. //  ================ createFireworks ================
  2414. //
  2415. //  SYNOPSIS
  2416. //      Create the F/X fireworks clip effect.
  2417. //
  2418. //    ARGUMENTS
  2419. //        string $fireworksName           -- the fireworks base name
  2420. //        int    $numRocketTrailsColors   -- number of trail colors/cones 
  2421. //        int    $numSparksColors         -- number of burst sparks colors/cones
  2422. //        string $trailColorCreationProc  
  2423. //        string $sparksColorCreationProc
  2424. //        int    $numRockets            
  2425. //        vector $launchPosition       
  2426. //        vector $burstAreaCenter 
  2427. //        vector $burstAreaExtents
  2428. //        float  $firstLaunchFrame
  2429. //        float  $launchRate
  2430. //        float  $minFlightTimeInFrames
  2431. //        float  $maxFlightTimeInFrames
  2432. //
  2433. //    RETURN
  2434. //        $fireworksGroup               -- name of the top level fireworks group
  2435. //
  2436. proc string createFireworks(string $fireworksName, 
  2437.                             int $numRocketTrailsColors,
  2438.                             int $numSparksColors,
  2439.                             string $trailColorCreationProc,
  2440.                             string $sparksColorCreationProc,
  2441.                             int $numRockets,
  2442.                             vector $launchPosition,
  2443.                             vector $burstAreaCenter,
  2444.                             vector $burstAreaExtents,
  2445.                             float $firstLaunchFrame,
  2446.                             float $launchRate,
  2447.                             float $minFlightTimeInFrames,
  2448.                             float $maxFlightTimeInFrames)
  2449.  
  2450. {
  2451.     global string $gFwSparksShaders[];
  2452.     global string $gFwTrailShaders[];
  2453.  
  2454.     clear($gFwSparksShaders);
  2455.     clear($gFwTrailShaders);
  2456.  
  2457.     string $returns[];
  2458.  
  2459.     // Determine a base name for the fireworks.
  2460.     //
  2461.     if (size($fireworksName) == 0)
  2462.     {
  2463.         $fireworksName = "Fireworks";
  2464.     }
  2465.  
  2466.     string $names[] = `ls $fireworksName`;
  2467.     if (size($names) > 0)
  2468.     {
  2469.         string $name = getUniqueName($fireworksName);
  2470.         $fireworksName = $name;
  2471.     }
  2472.  
  2473.     // Start the top level fireworks group and create its main attributes.
  2474.     //
  2475.     $fireworksGroup = `group -n $fireworksName -empty`;
  2476.     createFireworksAttributes($fireworksGroup);
  2477.  
  2478.     // Create the rockets particles and their gravity field.
  2479.     //
  2480.     $returns = createRockets($fireworksName,
  2481.                             $fireworksGroup,
  2482.                             $numRockets,
  2483.                             $launchPosition,
  2484.                             $burstAreaCenter,
  2485.                             $burstAreaExtents,
  2486.                             $firstLaunchFrame,
  2487.                             $launchRate,
  2488.                             $minFlightTimeInFrames,
  2489.                             $maxFlightTimeInFrames);
  2490.  
  2491.     string $rocketsShape = $returns[0];
  2492.     string $rocketsGroup = $returns[1];
  2493.     string $rocketsGrav  = $returns[2];
  2494.     string $rocketsLaunchPosShape = $returns[3];
  2495.  
  2496.     // Enable the display/selection handle on the fireworks main group, and set
  2497.     // it to be the centroid of the launch position particle.
  2498.     //
  2499.     setAttr  ($fireworksGroup + ".displayHandle") true;
  2500.     connectAttr
  2501.         ($rocketsLaunchPosShape + ".centroid")
  2502.         ($fireworksGroup + ".selectHandle");
  2503.  
  2504.     string $rocketTrailEmitter =
  2505.             createRocketTrailsEmitter($fireworksName, $rocketsShape);
  2506.  
  2507.     // Set the "main" colors for each of the rocket bursts. (At runtime, each
  2508.     // burst will get three colors: the "main" one and the ones adjacent to it.
  2509.     // The three colors will be randomly assigned to each spark in the burst.
  2510.     //
  2511.     setBurstColorIndices($rocketsShape, $numSparksColors);
  2512.  
  2513.     // Create the rocket trail sparks and connect them to the rocket sparks
  2514.     // emitter.
  2515.     //
  2516.     clear($returns);
  2517.     $returns = createRocketTrails($fireworksGroup,
  2518.                                     $fireworksName, 
  2519.                                     $rocketsShape,
  2520.                                     $trailColorCreationProc,
  2521.                                     $numRocketTrailsColors);
  2522.  
  2523.     string $rocketTrailShape = $returns[0];
  2524.     string $rocketSparksGroup = $returns[1];
  2525.     string $rocketTrailInstancer = $returns[2];
  2526.  
  2527.     connectDynamic -em $rocketTrailEmitter $rocketTrailShape;
  2528.  
  2529.     select -cl;
  2530.  
  2531.     // Create the particle shape that will hold the fireworks sparks when the
  2532.     // firework explodes.
  2533.     //
  2534.     clear($returns);
  2535.     $returns = createBurstSparks($fireworksGroup,
  2536.                                     $fireworksName, 
  2537.                                     $rocketsShape,
  2538.                                     $sparksColorCreationProc,
  2539.                                     $numSparksColors);
  2540.  
  2541.     string $burstSparksShape = $returns[0];
  2542.     string $burstSparksInstancer = $returns[1];
  2543.     string $sparksGroup = $returns[2];
  2544.  
  2545.  
  2546.     // Create the runtime expression for the rockets. (It has to be
  2547.     // created after the sparks are created, as it contains references
  2548.     // to the sparks instancer.
  2549.     //
  2550.     createRocketsExpression($rocketsShape, 
  2551.                             $burstSparksShape, 
  2552.                             $burstSparksInstancer,
  2553.                             $rocketTrailEmitter,
  2554.                             $rocketsGrav );
  2555.  
  2556.     // Group rocket, rocket sparks group and burst sparks group under the
  2557.     // fireworks group.
  2558.     //
  2559.     parent $rocketsGroup $sparksGroup $rocketSparksGroup $fireworksGroup;
  2560.  
  2561.     // Create expression for setting geometry vs particle display for
  2562.     // the rocket trails and burst sparks.  And set the seed for
  2563.     // starting the random number generator
  2564.     //
  2565.     string $exprString;
  2566.     $exprString =
  2567.         ( $rocketTrailShape + ".visibility = 1 - displayGeometry;\n"
  2568.         + $rocketTrailInstancer + ".visibility = displayGeometry;\n"
  2569.         + $burstSparksShape + ".visibility = 1 - displayGeometry;\n"
  2570.         + $burstSparksInstancer + ".visibility = displayGeometry;\n"
  2571.         + "float $animStart = `playbackOptions -q -min`;\n" 
  2572.         + "if (frame < $animStart+1) seed(1);");
  2573.         
  2574.     expression -n ($fireworksName + "Expr")
  2575.         -s $exprString
  2576.         -o $fireworksGroup;
  2577.  
  2578.     // Connect the Fireworks attributes to the attributes of the various
  2579.     // objects.  
  2580.     //
  2581.     connectFireworksAttributes($fireworksName,
  2582.                                 $fireworksGroup,
  2583.                                 $rocketsShape, 
  2584.                                 $rocketTrailShape, 
  2585.                                 $burstSparksShape,
  2586.                                 $rocketTrailEmitter);
  2587.  
  2588.     clear($gFwSparksShaders);
  2589.     clear($gFwTrailShaders);
  2590.  
  2591.     // Create the position locators that will be used to edit the start
  2592.     // and burst positions of the rockets.
  2593.     //
  2594.     string $manipNames[] = createRocketPositionLocators($fireworksName,
  2595.                                                         $rocketsShape);
  2596.  
  2597.     parent $manipNames[0] $manipNames[1] $fireworksGroup;
  2598.  
  2599.     hide $manipNames[0];
  2600.     hide $manipNames[1];
  2601.  
  2602.     // Create lights to illumine the RocketSparks and to illumine the
  2603.     // landscape from the Sparks.
  2604.     //
  2605.     // createRocketTrailsGlow();
  2606.     // createBurstSparksGlow();
  2607.  
  2608.     // 
  2609.     // Select the top level fireworks group
  2610.     //
  2611.     select -r $fireworksGroup;
  2612.  
  2613.     return $fireworksGroup;
  2614. }
  2615.  
  2616.  
  2617. //
  2618. //  ================ fireworks ================
  2619. //
  2620. //  SYNOPSIS
  2621. //      Wrapper to createFireworks() to create the F/X fireworks clip effect.
  2622. //        The wrapper is created in order to surround the main procedure in a
  2623. //        catch procedure so that if any fatal errors occur, full cleanup will
  2624. //        be done.
  2625. //
  2626. //    ARGUMENTS
  2627. //        string $fireworksName         -- the fireworks base name
  2628. //        int    $numRocketTrailsColors -- number of trail colors/cones
  2629. //        int    $numSparksColors       -- number of burst sparks colors/cones
  2630. //        string $trailColorCreationProc,
  2631. //        string $sparksColorCreationProc,
  2632. //        int $numRockets,
  2633. //        float $launchPositionX
  2634. //        float $launchPositionY
  2635. //        float $launchPositionZ
  2636. //        float $burstAreaCenterX
  2637. //        float $burstAreaCenterY
  2638. //        float $burstAreaCenterZ
  2639. //        float $burstAreaExtentX
  2640. //        float $burstAreaExtentY
  2641. //        float $burstAreaExtentZ
  2642. //        float $firstLaunchFrame
  2643. //        float $launchRate
  2644. //        float $minFlightTimeInFrames
  2645. //        float $maxFlightTimeInFrames
  2646. //
  2647. //    RETURN
  2648. //        $fireworksGroup   -- name of the top level fireworks group
  2649. //
  2650. global proc string fireworks(string $fireworksName, 
  2651.                         int $numRocketTrailsColors,
  2652.                         int $numSparksColors,
  2653.                         string $trailColorCreationProc,
  2654.                         string $sparksColorCreationProc,
  2655.                         int $numRockets,
  2656.                         float $launchPositionX,
  2657.                         float $launchPositionY,
  2658.                         float $launchPositionZ,
  2659.                         float $burstAreaCenterX,
  2660.                         float $burstAreaCenterY,
  2661.                         float $burstAreaCenterZ,
  2662.                         float $burstAreaExtentX,
  2663.                         float $burstAreaExtentY,
  2664.                         float $burstAreaExtentZ,
  2665.                         float $firstLaunchFrame,
  2666.                         float $launchRate,
  2667.                         float $minFlightTimeInFrames,
  2668.                         float $maxFlightTimeInFrames)
  2669. {
  2670.     if( `licenseCheck -type complete` == 0 )
  2671.     {
  2672.         warning("You are not licensed to use the Fireworks Effect.");
  2673.         return "";
  2674.     }
  2675.  
  2676.     vector $launchPosition = 
  2677.                 <<$launchPositionX, $launchPositionY, $launchPositionZ>>;
  2678.     vector $burstAreaCenter = 
  2679.                 <<$burstAreaCenterX, $burstAreaCenterY, $burstAreaCenterZ>>;
  2680.     vector $burstAreaExtents = 
  2681.                 <<$burstAreaExtentX, $burstAreaExtentY, $burstAreaExtentZ>>;
  2682.  
  2683.     // Save the names of all objects existing before the fireworks are created, so
  2684.     // we know what not to delete if the fireworks creation fails.
  2685.     //
  2686.     string $origSceneObjects[] = `ls`;
  2687.  
  2688.     string $fireworksGroup;
  2689.  
  2690.     // catch returns true if execution of the command produces an error.
  2691.     //
  2692.     if (catch($fireworksGroup = createFireworks($fireworksName, 
  2693.                             $numRocketTrailsColors,
  2694.                             $numSparksColors,
  2695.                             $trailColorCreationProc,
  2696.                             $sparksColorCreationProc,
  2697.                             $numRockets,
  2698.                             $launchPosition,
  2699.                             $burstAreaCenter,
  2700.                             $burstAreaExtents,
  2701.                             $firstLaunchFrame,
  2702.                             $launchRate,
  2703.                             $minFlightTimeInFrames,
  2704.                             $maxFlightTimeInFrames)))
  2705.     {
  2706.         warning( "Creation of the fireworks effect failed.  Deleting Fireworks objects.");
  2707.  
  2708.         // Get all objects now currently in the scene and delete any that were not
  2709.         // here before we started creating the fireworks.
  2710.         //
  2711.         string $currSceneObjects[] = `ls`;
  2712.  
  2713.         for ($i = 0; $i <  size($currSceneObjects); $i++)
  2714.         {
  2715.             int $found = 0;
  2716.             for ($j = 0; $j < size($origSceneObjects); $j++)
  2717.             {
  2718.                 if ($currSceneObjects[$i] == $origSceneObjects[$j])
  2719.                 {
  2720.                     $found = 1;
  2721.                     break;
  2722.                 }
  2723.             }
  2724.             // Check for whether the object exists because it may have been deleted
  2725.             // if its parent has already been deleted.
  2726.             //
  2727.             if ($found == 0 && objExists($currSceneObjects[$i]))
  2728.             {
  2729.                 delete $currSceneObjects[$i];
  2730.             }
  2731.         }
  2732.  
  2733.         return "";
  2734.     }
  2735.  
  2736.     return $fireworksGroup;
  2737. }
  2738.  
  2739.  
  2740.  
  2741.  
  2742.